i am quite new to iOS and Swift and now i want to use Neo4j, a Graph Database for persistent saving of my data.
I know there's a Lib named Theo for connecting to the Graph Database, but i want to use the REST API. Now i am not sure how to handle the HTTP-Request in order to get data from Database to my iOS-App written in Swift?
Anyone has some helpful clues for my?
Thanks hannes
The Theo library that you mention is a wrapper around the Neo4j REST API. The benefit of using such a library is that it takes care of things like JSON serialization, error handling, adding the correct headers to HTTP requests, etc.
You can use Cypher with the transactional Cypher REST API endpoint using Swift like this (Note: this uses the Alamofire library for handling the HTTP request.):
import Alamofire
let cypherStatement = "CREATE (:Person {name: 'Bob'})-[:LIKES]->(pizza:Food {type: 'Pizza'})<-[:LIKES]-(:Person {name: 'William'}) WITH pizza MATCH (p:Person)-[:LIKES]->(pizz) RETURN p.name AS name"
let endpoint = "http://localhost:7474/db/data/transaction/commit"
let requestBody = [
"statements": [
[
"statement": cypherStatement
]
]
]
Alamofire.request(.POST, endpoint, parameters: requestBody, encoding: .JSON)
.responseJSON { response in
debugPrint(response)
}
Which logs:
[Request]: <NSMutableURLRequest: 0x7fafcb637320> { URL: http://localhost:7474/db/data/transaction/commit }
[Response]: <NSHTTPURLResponse: 0x7fafcb81d480> { URL: http://localhost:7474/db/data/transaction/commit } { status code: 200, headers {
"Access-Control-Allow-Origin" = "*";
"Content-Length" = 91;
"Content-Type" = "application/json";
Date = "Fri, 18 Dec 2015 19:29:41 GMT";
Server = "Jetty(9.2.z-SNAPSHOT)";
} }
[Data]: 91 bytes
[Result]: SUCCESS: {
errors = (
);
results = (
{
columns = (
name
);
data = (
{
row = (
Bob
);
},
{
row = (
William
);
}
);
}
);
}
Edit
Making the same request without using Alamofire would look like this:
let cypherStatement = "CREATE (:Person {name: 'Bob'})-[:LIKES]->(pizza:Food {type: 'Pizza'})<-[:LIKES]-(:Person {name: 'William'}) WITH pizza MATCH (p:Person)-[:LIKES]->(pizz) RETURN p.name AS name"
let endpoint = "http://localhost:7474/db/data/transaction/commit"
let requestBody = [
"statements": [
[
"statement": cypherStatement
]
]
]
guard let url = NSURL(string: endpoint) else {
print("Error")
return true
}
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.addValue("application/json",forHTTPHeaderField: "Content-Type")
request.addValue("application/json",forHTTPHeaderField: "Accept")
do{
let jsonData = try NSJSONSerialization.dataWithJSONObject(requestBody, options: .PrettyPrinted)
request.HTTPBody = jsonData
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in
let result: NSDictionary
do {
result = try NSJSONSerialization.JSONObjectWithData(data!,
options: []) as! NSDictionary
} catch {
print("error trying to convert data to JSON")
return
}
print("\(result)")
})
task.resume()
} catch let error as NSError {
print("JSON serialization error")
return true
}
Related
I have a working python script that gets cookies from a site. I want to convert it to Swift script, but I do something wrong and can not understand where.
#!/usr/bin/env python
import warnings
import json
import os
import requests
auth_login='someName'
auth_password='somePassword'
passport_params = {'from': 'passport', 'retpath': 'https://192.168.0.1/passport?mode=passport', 'passwd': auth_password, 'login': auth_login, 'display': 'page'}
passport_session = requests.session()
passport_error_passwd = u"wrong pass"
passport_error_login = u"no such users"
print('Connecting to passport...')
try:
passport_request = passport_session.post('https://192.168.0.1/passport?mode=auth', data=passport_params, verify=False)
except:
print('Error connecting')
if os.name == 'nt':
os.system("pause")
exit(1)
else:
print('Authentication successful')
passport_cookies = requests.utils.dict_from_cookiejar(passport_session.cookies)
print('cookies=')
print(passport_cookies)
I am not sure that my post request works correctly, so my cookies are empty.
let auth_login = "someName"
let auth_password = "somePassword"
let url = URL(string: "https://192.168.0.1/passport?mode=auth")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.httpMethod = "POST"
let parameters = [
"from": "passport",
"retpath": "https://192.168.0.1/passport?mode=passport",
"passwd": auth_password,
"login": auth_login,
"display": "page"
]
request.httpBody = try? JSONSerialization.data(withJSONObject: parameters)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard
// let data = data,
let response = response as? HTTPURLResponse,
let fields = response.allHeaderFields as? [String: String]
else {
print("error", error ?? URLError(.badServerResponse))
return
}
let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields, for: url)
print("cookies:\n\(cookies)")
guard (200 ... 299) ~= response.statusCode else {
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
}
task.resume()
Your python code is sending these:
passport_params = {'from': 'passport', 'retpath': 'https://192.168.0.1/passport?mode=passport', 'passwd': auth_password, 'login': auth_login, 'display': 'page'}
as POST data payload.
While your Swift code
let parameters = [
"from": "passport",
"retpath": "https://192.168.0.1/passport?mode=passport",
"passwd": auth_password,
"login": auth_login,
"display": "page"
]
request.httpBody = try? JSONSerialization.data(withJSONObject: parameters)
is doing that as JSON http body.
So these requests are very different, hence the problem you encounter.
Have a look at this answer how to configure POST data in a request in Swift .
I am using a asp.net back end with a login end point but no matter what I DO in the swift version of this code I get a 415 when I use it in .net and sharp the api works am not sure what am doing wrong here.
And yes I have enabled transport protocol but its not decoding the jwt token correctly for me in swift
Basically the end point returns the jet token used for accessing the api in an object
let jwtAccessToken: String = ""
let urlString = "http://url.com/login" *** hidden for security
purposes but is correct ****
func CallWebApi()
{
// create the url with URL
let url = URL(string: urlString)! // change server url accordingly
let parameters: [String: Any] = [ "username":
"user1#domain.com", "password": "pass1"]
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded",
forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "Post"
do {
request.httpBody = try
JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
return
}
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
guard
let data = data,
let response = response as? HTTPURLResponse,
error == nil
else {
// check for fundamental networking error
print("error", error ?? URLError(.badServerResponse))
return
}
guard (200 ... 299) ~= response.statusCode else {
// check for http errors
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
// do whatever you want with the `data`, e.g.:
do {
let responseObject = data
print(responseObject)
} catch {
print(error)
// parsing error
if let responseString = String(data: data, encoding: .utf8) {
print("responseString = \(responseString)")
} else {
print("unable to parse response as string")
}
}
}
task.resume()
}
MyModel is basically a string
import Foundation
class AuthenticationResponse: ObservableObject {
#Published var jwtToken: String
init(jwtToken: String) {
self.jwtToken = jwtToken
}
}
I think 20 years of c sharp in not helping and am doing things it way and not the swift way if someone could advice be great.
Also in csharp we were told its not great in keeping alive the http client as can degrade performance is this the same for swift and if any library's you can recommend makes the code a bit neater the api has swagger docs enabled.
Edit 3
Example response expected back
{
"id": "b181104e-ba3e-4dba-b124-4bb4a3873b17",
"firstName": "user1",
"lastName": "lastname",
"username": "user1lastname#domainname.com",
"playerId": 0,
"jwtToken": "token in is here",//hidden for security
"error": {
"eventName": null,
"errorMessage": null,
"errorDate": null,
"statusCode": null,
"json": null
},
"refreshToken": null
}
I typically send this to the end point from C sharp
{
"username": "user1#domain.com",
"password": "pass1"
}
What I found I had to do was this
let decoder = JSONDecoder()
let responseObject = try
decoder.decode(AuthenticationResponse.self, from: data)
print(responseObject)
And change my class to be off this
import Foundation
struct AuthenticationResponse: Codable {
var jwtToken: String
}
After I done that I got the expected string back but my question is how does one get this to run correctly its completing before I think I need await but also where is it best to stored the jwttoken?
I'm having a problem sending a POST request with Alamofire.
I need to send the usser and password fields as application/x-www-form-urlencode and also some query data in the url.
I am creating a URLRequest to handle the process, but I'm getting always a 400 response from the server, so I guess the problem must be in the way I create the request.
This is the example in Postman:
I need to send a param in the url and two more in as application/x-www-form-urlencode
Postman 1 - Parameters
Postman 2 - ContentType
I need to do this (that i have in Android)
#FormUrlEncoded
#POST(Constants.AUTH_LDAP)
Call<ResponseBody> authLdap(
#Query(value = Constants.PARAM_REQ, encoded = true) String req,
#Field(Constants.PARAM_LOGIN) String login,
#Field(Constants.PARAM_PASSWORD) String password
);
And this is what I have in swift
let queryParamters = [Constants.Params.PARAM_REQ:req]
let headers = ["Content-Type": "application/x-www-form-urlencoded"]
let fieldParameters = [
Constants.Params.PARAM_LOGIN : user,
Constants.Params.PARAM_PASSWORD : pass]
let url = URL(string: Constants.EndPoints.AUTH_LDAP)
let request = URLRequest(url: url!)
let encoding = try URLEncoding.default.encode(request, with: queryParamters as Parameters)
let encodingpa = try URLEncoding.httpBody.encode(request, with: fieldParameters as Parameters)
var urlRequest = encodingpa
urlRequest.url = encoding.url
urlRequest.allHTTPHeaderFields = headers
Alamofire.request(urlRequest).responseData(completionHandler: { response in
switch response.result {
case .success:
print("sucess")
print(response.response)
case .failure(let error):
print(error)
}
})
Thanks for your help.
Try to create url from queryParameters using URLComponents like
var urlComponents = URLComponents(string: Constants.EndPoints.AUTH_LDAP)!
urlComponents.queryItems = [
URLQueryItem(name: Constants.Params.PARAM_REQ, value: req)
]
let headers = ["Content-Type": "application/x-www-form-urlencoded"]
var request = URLRequest(url: urlComponents.url!)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: fieldParameters)
request.allHTTPHeaderFields = headers
Alamofire.request(request).responseJSON { response in
}
I am trying to make a request to Google Cloud NLP API to obtain sentiment analysis for a piece of text. I used Postman to design the correct request, and I was able to get a valid response using Postman. However, when I try to make the same request from Swift, it gives me an error. The error and code snippet used to make the request is shown below.
func sendAPIRequest(with text: String){
print("Text: ", text)
let jsonRequest = [
[
"document":[
"type":"PLAIN_TEXT",
"language": "EN",
"content":"'Lawrence of Arabia' is a highly rated film biography about British Lieutenant T. E. Lawrence. Peter O'Toole plays Lawrence in the film."
],
"encodingType":"UTF8"
]
]
let jsonObject = JSON(jsonRequest)
let headers: HTTPHeaders = [
"X-Ios-Bundle-Identifier": "\(Bundle.main.bundleIdentifier ?? "") ",
"Content-Type": "application/json"
]
let APIRequest = Alamofire.request("https://language.googleapis.com/v1/documents:analyzeSentiment?key=\(gCloudAPIKey)", method: .post , parameters: jsonRequest as? [String: Any], encoding: JSONEncoding.default , headers: headers).responseJSON { (response) in
print(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}
Error:
JSON: {
error = {
code = 400;
details = (
{
"#type" = "type.googleapis.com/google.rpc.BadRequest";
fieldViolations = (
{
description = "Must have some text content to annotate.";
field = "document.content";
}
);
}
);
message = "One of content, or gcs_content_uri must be set.";
status = "INVALID_ARGUMENT";
};
}
Sorry. Solved it. My jsonRequest should be of type Parameters according to Alamofire.
let jsonRequest: Parameters =
[
"document":[
"type":"PLAIN_TEXT",
"language": "EN",
"content":"\(text)"
],
"encodingType":"UTF8"
]
I need to send JSON format in post api using alamofire in swift. Way data need to send is
{
"data": [{
"id": "1015683997",
"name": "Pawel"
}, {
"id": "108350039247",
"name": "Randeep"
}, {
"id": "115607797616",
"name": "Mohit"
}]
}
And way i am able to generate as of now is:
["data": {
data = (
{
id = 101583997;
name = "Pawel";
},
{
id = 108359247;
name = "Randeep";
},
{
id = 11567616;
name = "Mohit ";
}
);
}
]
Using the below mentioned way to generate json format.
for i in 0..<self.arrFriendsList.count
{
let dictFrndList:[String: AnyObject] = [
"id" : arrFriendsList[i].valueForKey("id")!,"name" : arrFriendsList[i].valueForKey("name")!
]
arrFrndList.addObject(dictFrndList)
}
Then,
let dictFBFrndList: [String: AnyObject] = [
"data":arrFrndList
]
Then,
let params: [String: AnyObject] = [
"access_token" : accessToken,
"data":dictFBFriends
]
So, please guide me. i have already spend more than a day on it. thanks in advance.
I second what Larme said, you should post "arrFrndList" at the params with key "data" but not dictFBFrndList.
Managed this way
let jsonData = try! NSJSONSerialization.dataWithJSONObject(arrFriendsList, options: NSJSONWritingOptions.PrettyPrinted)
let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding)! as String
print(jsonString)
let params: [String: AnyObject] = [
"access_token" : accessToken,
"data":jsonString
]
let options = NSJSONWritingOptions()
let data = try? NSJSONSerialization.dataWithJSONObject(parameters, options: options)
let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string:"http:/myurl.com")
if let str = NSString(data: data!, encoding: NSUTF8StringEncoding) as? String {
print(str)
}
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
mutableURLRequest.setValue("your_access_token", forHTTPHeaderField: "Authorization")
mutableURLRequest.HTTPBody = data
mutableURLRequest.HTTPMethod = "POST"
Alamofire.request(mutableURLRequest).responseJSON(){
json in
if let value = json.result.value {
let _json = JSON(value)
if (!_json.isEmpty){
print(_json)
}
}
}
you can check how to seen your JSON array this code. Look at 5th cell code. This code will write your json array for you in output screen and you can coppy from there and paste in here then you will see how to send data.
Maybe this way can help you .