I am trying to upload a string array to the Edamam API that uses the data from my local database. For the API, I need to post an array of strings with a label "ingr" so that it would recognize it. I should be able to enter the "\(mass) grams \(name)" of each ingredient and get a response that would analyze the macronutrients of said ingredient. The code I'm using:
API Config:
func getData(strURL: String, dictParam: Dictionary<String, Any>, completion: #escaping(Dictionary<String, AnyObject>)->()) {
var request = URLRequest(url: URL(string: strURL)!)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: dictParam, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
print(response!)
do {
let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary<String, AnyObject>
print(json)
completion(json)
} catch {
print("error")
}
})
task.resume()
}
API call:
let dictParams = ["ingr":["100 grams apple"]] as Dictionary<String, Any>
APICalling.shared.getData(strURL: baseURL, dictParam: dictParams) { (response) in
action()
}
}
When using the Rested app, I can upload as many ingredients as I want and will get the correctly formatted response, everything is dandy. However, when I try to implement the code in my app, it prints "error":conflict. Any ideas how I can properly implement the API call?
Found a fix to the issue. Added some headers to my request, everything works fine now.
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("*/*", forHTTPHeaderField: "Accept")
request.addValue("no-cache", forHTTPHeaderField: "Cache-Control")
request.addValue("api.edamam.com", forHTTPHeaderField: "Host")
request.addValue("gzip, deflate", forHTTPHeaderField: "Accept-Encoding")
request.addValue("keep-alive", forHTTPHeaderField: "Connection")
Related
This is the approach I am using to get the tweets via twitter new v2 filter search API after getting the bearer token.
static func callFilterSearch() {
let Url = String(format: "https://api.twitter.com/2/tweets/search/stream?place.fields=contained_within&user.fields=location&tweet.fields=geo")
guard let serviceUrl = URL(string: Url) else { return }
var request = URLRequest(url: serviceUrl)
request.httpMethod = "GET"
request.setValue("Bearer MY BEARER TOKEN", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("gzip, deflate, br", forHTTPHeaderField: "Accept-Encoding")
request.setValue("keep-alive", forHTTPHeaderField: "Connection")
request.setValue("*/*", forHTTPHeaderField: "Accept")
request.setValue("v2FilteredStreamPython", forHTTPHeaderField: "User-Agent")
let sessionConfig = URLSessionConfiguration.default
sessionConfig.networkServiceType = .responsiveData
let session = URLSession(configuration: sessionConfig)
session.dataTask(with: request) { (data, response, error) in
if let data = data {
do {
let teamJSON = try JSONSerialization.jsonObject(with: data, options: [.allowFragments, .fragmentsAllowed, .mutableContainers, .mutableLeaves])
print("teamJSON: \(teamJSON)")
} catch let error {
print("error: \(error)")
}
}
}.resume()
}
Unfortunately I am not successful in getting the tweets this way. However the python client that twitter has put in github repo seems to work just fine and is spewing tweets as expected.
Please help me out here
I'm sending this data to a server and I would like it to respond. Tried this code but it doesn't work.
let parameters = [ "imie" : (imie), "nazwisko" : (nazwisko), "telefon" : (telefon), "adres" : (adres), "miasto" : (miasto), "kod" : (kod), /*"powiat" : (powiat), "wojewodztwo" : (wojewodztwo),*/ "opis" : (selectedvalue) ]
let url = URL(string: "http://www.hetman.pl/post1.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.setBodyContent(parameters)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
if error != nil{
return
}
do{
let t = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print(t as? String)
} catch{
print("Error 43 -> \(error)")
}
}
Request is correct and server is processing data properly, but response gives me this error:
Error 43 -> Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 3." UserInfo={NSDebugDescription=Invalid value around character 3.}
So the question is, how do I get the data back from server?
Please check if response from this url is JSON only. You might be getting XML or string response.
Try below line if you are getting String response :
let stringResponse = String(data: data!, encoding: String.Encoding.utf8) as String!
Try below line if you are getting XML response :
let xmlDictionary = try XMLSerialization.xmlObject(with: data!) as? [String: Any]
Why there is to content type?
// code 3840, data.length = 1 byte, so empty and no error
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
Remove first content-type header field and try with only:
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
This is my feedback json string:
{"name":"abc", "cardNumber":"1234567890", "data": [{A data},{B data}...]}
I use this function to send data, then get json and encode:
func uploadData(word:String){
var request = URLRequest(url: url!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30)
request.httpMethod = "POST"
request.httpBody = word.data(using: .utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession(configuration: URLSessionConfiguration.default)
session.dataTask(with: request, completionHandler: {(data, response, error) in
if let data = data{
do{
let data = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
print(data) //I want to know what is this so I print
}catch{
print(error.localizedDescription)
}
}
}).resume()
}
But the console always says:The data couldn't be read because it isn't in the correct format
This json can be format and read in android if I use JSONObject.getJSONArray("myValue")...
I try to use print(data)(without json encode) to show if there is any data in feedback and I get 400byte in console, so I'm sure there is data send back to me.
UPDATE 12/28:
{"name":"abc",
"cardNumber":"1234567890",
"data": [{day:20171228, time: 09:10:11},
{day:20171226, time: 20:00:12},
{day:20171227, time: 15:30:22}
]
}
I'm sure this json can be read in android, the receiver and sender I use is vb.net, it use sendingString = JsonConvert.SerializeObject(JSONClass) to become json string, then convert to byte to send out.
UPDATE 12/28 new
After trying so much, I found string can get the feedback, but the value of name is Chinese word, other value is English and number, only name is unreadable, now I'm checking which String.Encoding will work, then if I encode it success, I will try to format to json Array.
Can you try this one?
func uploadData(word:String){
var request = URLRequest(url: url!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30)
request.httpMethod = "POST"
request.httpBody = word.data(using: .utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession(configuration: URLSessionConfiguration.default)
session.dataTask(with: request, completionHandler: {(data, response, error) in
if let data = data{
if let returnData = String(data: data!, encoding: .utf8) {
print(returnData)
} else {
print("Invalid Data Coming")
}
do{
let data = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject]
print(data) //I want to know what is this so I print
}catch{
print(error.localizedDescription)
}
}
}).resume()
}
I'm trying to send an email with the Mailjet API v3 with a http post request but I'm getting an error 400.
I used the exact same body with success in Javascript, but I guess the error 400 is related with it...
Any ideas ?
var recipients = [Any]()
recipients.append(["Email": "email#gmail.com"])
var body: [String: Any] = [
"FromEmail": "anEmail#gmail.com",
"FromName": "Me",
"Subject": "YEEES",
"Text-part": "Greetings from IOS ;)",
"Recipients": recipients
]
var request = URLRequest(url: self.apiURL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Authorization", forHTTPHeaderField: "Basic <keysInBase64>")
do {
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
}
catch {
print("error during JSON serialization")
dump(error)
return
}
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
print(error)
print(response)
print(data)
})
task.resume()
Headers was wrong...
I was doing :
request.setValue("Authorization", forHTTPHeaderField: "Basic <keysInBase64>")
Instead of :
request.setValue("Basic <keysInBase64>", forHTTPHeaderField: "Authorization")
Using the Charles Proxy as suggested by #LouFranco, I was able to find the mistake.
I'm trying to send a Patch request with a serialized JSON Body.
For some reason the server is not able to receive the body properly. I have a feeling that there seems to be a problem with the PATCH method in combination with the http request body.
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
var URL = B2MFetcher.urlForBooking(event.unique, bookingID: booking.unique)
let request = NSMutableURLRequest(URL: URL)
request.HTTPMethod = "PATCH"
// Headers
println(token)
request.addValue(token, forHTTPHeaderField: "Authorization")
request.addValue("gzip, identity", forHTTPHeaderField: "Accept-Encoding")
// JSON Body
let bodyObject = [
"op": "cancel"
]
var jsonError: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(bodyObject, options: nil, error: &jsonError)
/* Start a new Task */
let task = session.dataTaskWithRequest(request, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
completion(data: data, response:response , error: error)
})
task.resume()
You could try to add a Content-Type header to the request:
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
or use one of the other JSON Content-Type formats described here.
I tested it with an ExpressJS server and without the Content-Type header the server got an empty body, but with a Content-Type header it worked well.
in swift 3/4 :
let request = NSMutableURLRequest(url: NSURL(string: "http://XXX/xx/xxx/xx")! as URL)
request.httpMethod = "PATCH"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
do{
let json: [String: Any] = ["status": "test"]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
request.httpBody = jsonData
print("jsonData: ", String(data: request.httpBody!, encoding: .utf8) ?? "no body data")
} catch {
print("ERROR")
}
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
completion(false)
return
}
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(responseString)")
completion(true)
return
}
task.resume()
Simple Way to use patch without using HTTPBody
If you want to just use patch, you just need to change the value of the name of a specific user then it will be like:
let myurl = URL(string: "https://gorest.co.in/public-api/users/"+"\(id)?"+"name=abc")!
var request = URLRequest(url:myurl)
request.addValue("Bearer yourAuthorizationToken",forHTTPHeaderField:"Authorization")
request.httpMethod = "PATCH"
let dataTask = URLSession.shared.dataTask(with: request)
dataTask.resume()
Note: here "id" will be userId