Converting ecobee Alamofire request to use URLSession - swift

As a followup to my last question (Alamofire syntax for ecobee request), I would prefer to just use URLSession for the request.
Now I'm back to a request that times out with status 408 using the following code:
guard let url = URL(string: "https://api.ecobee.com/1/thermostat") else { return }
let jsonParameters = [ "selection": [ "selectionType": "registered", "selectionMatch": "" ] ]
let jsonData = try! JSONEncoder().encode(jsonParameters)
let jsonString = String(decoding: jsonData, as: UTF8.self)
let queryParameters = ["format": "json", "body": jsonString]
let headers: HTTPHeaders = [.authorization(bearerToken: AUTH_TOKEN), .contentType("text/json")]
var request = try! URLRequest(url: url, method: .get, headers: headers)
request.httpBody = try! JSONEncoder().encode(queryParameters)
URLSession.shared.dataTask(with: request) { (data, resp, err) in
debugPrint(String(data: data!, encoding: .utf8)!)
}.resume()
I suspect I'm not adding the query parameters correctly.

I found my own solution using UrlComponents
func testRequest() {
guard var url = URLComponents(string: "https://api.ecobee.com/1/thermostat") else { return }
let jsonParameters = [ "selection": [ "selectionType": "registered", "selectionMatch": "" ] ]
let jsonData = try! JSONEncoder().encode(jsonParameters)
let jsonString = String(decoding: jsonData, as: UTF8.self)
let queryParameters = ["format": "json", "body": jsonString]
url.queryItems = queryParameters.map { URLQueryItem(name: $0.key, value: $0.value) }
let headers = [
"Authorization": "Bearer \(core.accessToken)",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
]
var request = URLRequest(url: url.url!)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
URLSession.shared.dataTask(with: request) { (data, resp, err) in
debugPrint(String(data: data!, encoding: .utf8)!)
}.resume()
}

Related

Swift 5, make http post request

How can I do attached "postman operation" on Swift 5? i would like to use this code for login with rest service on ios(iphone).
Below is the code for Post Method,using URLSession
let Url = String(format: "http://10.10.10.53:8080/sahambl/rest/sahamblsrv/userlogin")
guard let serviceUrl = URL(string: Url) else { return }
let parameters: [String: Any] = [
"request": [
"xusercode" : "YOUR USERCODE HERE",
"xpassword": "YOUR PASSWORD HERE"
]
]
var request = URLRequest(url: serviceUrl)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
return
}
request.httpBody = httpBody
request.timeoutInterval = 20
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
}
Try this with Alamofire 4.x
let parameters: [String: Any] = [
"request": [
"xusercode" : "YOUR USERCODE HERE",
"xpassword": "YOUR PASSWORD HERE"
]
]
Alamofire.request("YOUR URL HERE", method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):
print(error)
}
}

Swift 4.2 code equivalent for SAP's Leonardo API [duplicate]

This question already has answers here:
Upload image with parameters in Swift
(3 answers)
Closed 4 years ago.
Getting HTTP 400 error while connecting with SAP Leonardo sandbox server using scene text recognition API
SAP provides boilerplate code in older version of swift. I have used curl command and the API and works. But its not working when I try and convert the code into swift 4.2 version. I am attaching the code below.
func connectWithSAP(photoURL : URL, photoData : String, sentImageData : Data){
if let myNewURL = URL(string: "https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition") {
var myRequest = URLRequest(url: myNewURL)
myRequest.addValue("multipart/form-data; --\(boundary)", forHTTPHeaderField: "Content-Type")
myRequest.addValue("application/json", forHTTPHeaderField: "Accept")
myRequest.addValue("xxxxxxxxxxx", forHTTPHeaderField: "APIKey")
myRequest.httpMethod = "POST"
myRequest.cachePolicy = .reloadIgnoringLocalCacheData
myRequest.timeoutInterval = 60.0
// Constructing the body of the request.
var data = Data()
var dataString = ""
dataString.append("--\(boundary)\r\n")
dataString.append(contentsOf: "Content-Disposition:form-data; name=\"files\"; filename=\"Image1.jpeg\" \r\n")
dataString.append(contentsOf: ";Content-Type:image/jpeg \r\n\r\n")
dataString.append(photoData)
dataString.append("--\(boundary) ----- \r\n")
data = dataString.data(using: .utf8)!
myRequest.httpBody = data
let task = URLSession.shared.dataTask(with: myRequest) { (data, response, error) in
if let error = error {
print(error)
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
print(error as Any)
// Getting output at this stage, which is shown below
return }
if let mimeType = httpResponse.mimeType,
mimeType == "application/json",
let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]
print(json as Any)
}catch {
print(error)
}
}
}
task.resume()
}
I am getting a following details in my response object
{ URL: https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition } { Status Code: 400, Headers {\n Connection = (\n \"keep-alive\"\n );\n \"Content-Length\" = (\n 131\n );\n \"Content-Type\" = (\n \"application/json\"\n );\n Date = (\n \"Sat, 16 Feb 2019 11:56:37 GMT\"\n );\n Server = (\n \"Werkzeug/0.14.1 Python/3.5.5\"\n );\n \"Strict-Transport-Security\" = (\n \"max-age=31536000; includeSubDomains; preload;\"\n );\n \"X-Vcap-Request-Id\" = (\n \"fea7037c-4e48-49d2-4be1-53b0dad0ee46\"\n );\n}
As you would see the status code is HTTP 400. Need some help in getting the right response and data from the server.
Most probably, the body data got messed up. Here's the working code:
let boundaryConstant = "----WebKitFormBoundary7MA4YWxkTrZu0gW"
let headers = [
"APIKey": "YourAPIKEY"
]
let contentType = "multipart/form-data; boundary=" + boundaryConstant
//API endpoint for API sandbox
var request = URLRequest(url: URL(string: "https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition")!)
//setting request method
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let path1 = Bundle.main.path(forResource: "your_image", ofType: "png")!
let url = URL(fileURLWithPath: path1)
let fileName = url.lastPathComponent
let data = try? Data(contentsOf: url)
let imageData = UIImage.init(data: data!)!
let pngData = UIImagePNGRepresentation(imageData)!
let mimeType = "image/png"
let boundaryStart = "--\(boundaryConstant)\r\n"
let boundaryEnd = "--\(boundaryConstant)--\r\n"
let fieldName = "files"
let contentDispositionString = "Content-Disposition: form-data; name=\"\(fieldName)\"; filename=\"\(fileName)\"\r\n"
let contentTypeString = "Content-Type: \(mimeType)\r\n\r\n"
var body = Data()
body.append(boundaryStart.data(using: .utf8)!)
body.append(contentDispositionString.data(using: .utf8)!)
body.append(contentTypeString.data(using: .utf8)!)
body.append(pngData)
body.append("\r\n".data(using: .utf8)!)
body.append(boundaryEnd.data(using: .utf8)!)
request.httpBody = body
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.setValue(String(body.count), forHTTPHeaderField: "Content-Length")
let dataTask = session.dataTask(with: request) { (data, response, error) in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
}
dataTask.resume()
You can also use Alamofire to upload image. It's way cleaner, don't need to play around with "body" much:
let headers: HTTPHeaders = [
"APIKey": "<<Your API KEY>>",
"Content-type": "multipart/form-data"
]
let parameters:[String: String] = [:] //any other parameters you need to send
let path1 = Bundle.main.path(forResource: "<<your_image>>", ofType: "<<png or jpeg>>")!
let url = URL(fileURLWithPath: path1)
let fileName = url.lastPathComponent
let data = try? Data(contentsOf: url)
let imageData = UIImage.init(data: data!)!
//converting it into png data
let pngData = UIImagePNGRepresentation(imageData)
let mimeType = "image/png"
let fieldName = "files"
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
if let data = pngData{
multipartFormData.append(data, withName: fieldName, fileName: fileName, mimeType: mimeType)
}
}, usingThreshold: UInt64.init(), to: "https://sandbox.api.sap.com/ml/scenetextrecognition/scene-text-recognition" , method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
print("Succesfully uploaded")
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
}
}

Swift URLSession doesn't behave like Curl or HTTParty

Background
I am working on a swift project with particle.io setup two legged auth part. Basically it's a POST request.
My issue is I can get the correct response by CURL and HTTParty. (Like below) but withURLSession` the response is 404.
By CURL
curl -X POST -u "abcd:secret" -d password=true -d email="wwsd#gmail.com" https://api.particle.io/v1/orgs/xxx/customers
By HTTParty
require 'HTTParty'
def register_spark_two_legged_user(query)
return HTTParty.post("https://api.particle.io/v1/orgs/xxx/customers", body: query, basic_auth:{"username":"abcd","password":"secret"})
end
query = {"email":"wwsd#gmail.com", "no_password":true}
json = register_spark_two_legged_user query
p json
I want to do it in Swift:
func twoLegged() {
let urlString = "https://api.particle.io/v1/orgs/xxx/customers"
let parameters = ["email":"wwsd#gmail.com","no_password":true] as [String : Any]
let userName = "abcd"
let password = "secret"
let loginString = userName+":"+password
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()
let url = URL(string: urlString)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
URLSession.shared.dataTask(with: url) { (data: Data?, response: URLResponse?, error: Error?) in
if let e = error {
print(e.localizedDescription)
} else {
let json = try? JSONSerialization.jsonObject(with: data!, options: [])
debugPrint(response as Any)
print(json)
}
}.resume()
Did I miss something? Thanks for the help. Here's a link might useful: community.particle.io
EDIT I changed the httpBody still the same not work.
var comp = URLComponents()
comp.queryItems = [
URLQueryItem(name: "no_password", value: "true"),
URLQueryItem(name: "email", value: "wwsd#gmail.com"),
]
request.httpBody = comp.query?.data(using: String.Encoding.utf8)
request.setValue("application/x-www-form-urlencode", forHTTPHeaderField: "Content-Type")
The output is
Optional({
error = "Not Found";
ok = 0;
})
In curl you are sending the data out as application/x-www-form-urlencoded, i.e. in the form
no_password=true&email=wwsd#gmail.com
But in Swift you are sending off the data as JSON.
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
# wrong: form data is expected, not JSON.
You could format to application/x-www-form-urlencoded using URLComponents and URLQueryItem:
var comp = URLComponents()
comp.queryItems = [
URLQueryItem(name: "no_password", value: "true"),
URLQueryItem(name: "email", value: "wwsd#gmail.com"),
]
request.httpBody = comp.query?.data(using: .utf8)
Also you did not pass the request into URLSession...
URLSession.shared.dataTask(with: request) { ... }
// ^~~~~~~ you were passing `url`.

Migrating from URLSession to Alamofire 4.3 in Swift 3, Encoding issue

What is the equivalent code of the following in Alamofire 4.3,Swift 3?
let baseUrl = "https://hiddenWebsite.com/api/index_load"
let url = URL(string: baseUrl)
let authObj = ["owner_id": "361900", "auth_key": "f408634ac1e14c08eebce46c34ab9db2", "device": "2", "version": "2.1.16"]
let infoObj = ["case": "feeds", "feeds_call_type": "init", "feed_type": "", "load_next_from": "1"]
let infoJSONData = try! JSONSerialization.data(withJSONObject: infoObj)
let infoStr = "info" + "=" + String(data: infoJSONData, encoding: String.Encoding.utf8)!
let authJSONData = try! JSONSerialization.data(withJSONObject: authObj)
let authStr = "auth" + "=" + String(data: authJSONData, encoding: String.Encoding.utf8)!
let combinedStr = infoStr + "&" + authStr
let reqdat = combinedStr.data(using: String.Encoding.utf8)
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.httpBody = reqdat
let defaultSession = URLSession.shared
defaultSession.dataTask(
with: request,
completionHandler: { data, response, error in
guard error == nil else {
print("Error while fetching data: \(error)")
return
}
guard let data = data,
let json = try? JSONSerialization.jsonObject(
with: data,
options: JSONSerialization.ReadingOptions.mutableContainers)
else {
print("Nil data received")
return
}
print(json)
}).resume()
I'm currently migrating from Apple's URLSession to Alamofire, but stuck at this point. I think this has something to do with encodingParameter.
This is my failed attempt. I also tried with URLEncoding.httpbody but didn't work.
let baseUrl = "https://hiddenWebsite.com/api/index_load"
let masterParameter = [
"auth": ["owner_id": "361900",
"auth_key": "f408634ac1e14c08eebce46c34ab9db2",
"device": "2",
"version": "2.1.16"],
"info": ["case": "feeds",
"feeds_call_type": "init",
"feed_type": "",
"load_next_from": "1"]
]
Alamofire.request(baseUrl, method: .post, parameters: masterParameter, encoding: JSONEncoding.default).responseJSON { (response) in
print(response)
}
The working postman equivalent of this looks like
Postman Screenshot
The encoding you are using it not valid JSON. Therefore, you cannot use JSON.default as your encoding type because it will encode your masterParameter dictionary as a valid JSON dictionary.
Instead, you either need to build your URLRequest manually as you have done in your first example and use the Alamofire.request(urlRequest) API, or you can create your own ParameterEncoding where you pass your data into it then encode the URLRequest.
Thanks cnoon for answering. The following code solved the issue:
public enum JSONError: Error {
case serializing(String)
case encoding
}
extension String: ParameterEncoding {
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try urlRequest.asURLRequest()
request.httpBody = self.data(using: String.Encoding.utf8)
return request
}
}
let encodedStr = try getEncodedString(parameters: masterParameter)
Alamofire.request(baseUrl, method: .post, parameters: [:], encoding: encodedStr).responseJSON { response in
print(response)
}
where getEncodedString(parameters:) is defined as:
func getEncodedString(parameters: Parameters) throws -> String {
var encodedStr = ""
for (key, value) in parameters {
let jsonData: Data
do {
jsonData = try JSONSerialization.data(withJSONObject: value)
}
catch(let error) {
throw JSONError.serializing(error.localizedDescription)
}
guard let encodedJsonString = String(data: jsonData, encoding: .utf8) else {
throw JSONError.encoding
}
let keyValueStr = key + "=" + encodedJsonString
encodedStr += keyValueStr + "&"
}
if encodedStr.characters.last == "&" {
encodedStr.remove(at: encodedStr.index(before: encodedStr.endIndex))
}
return encodedStr
}

NSData is nil After AsynchronousRequest In Swift

I am trying to add video data to the HTTP Request's body but sometimes video data is turning to nil but sometimes not. Is there anything to fix this situation? When I delete the app from my app and after doing simulation again, nothing happened.
#IBAction func post(sender: AnyObject) {
let videodata = NSData(contentsOfURL: videoURL!)
let headers = [
"authorization": "Token \(userToken!)",
"content-type": "/*/",
"content-disposition": "attachment;filename=deneme.mp4",
"cache-control": "no-cache"
]
let request = NSMutableURLRequest(URL: NSURL(string: "http://molocate.elasticbeanstalk.com/video/upload/")!,
cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData,
timeoutInterval: 10.0)
request.HTTPMethod = "POST"
request.allHTTPHeaderFields = headers
request.HTTPBody = videodata
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
//print(NSString(data: data!, encoding: NSUTF8StringEncoding))
do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)
print("Result -> \(result)")
let statue = result["result"] as! String
if(statue == "success"){
let videoId = result["video_id"] as! String
let videoUrl = result["video_url"] as! String
print(videoUrl)
let json = [
"video_id": videoId,
"video_url": videoUrl,
"caption": "This city is awesome:)",
"category": "travel",
"tagged_users": [["username": "amertturker"]],
"location": [
[
"id": "mekmaekfmaıhjagej3ıo45j3kt348t3gkg",
"latitude": "35.342643",
"longitude": "32.345236",
"name": "Milas Merkez Kafasına göre herkes",
"address": "Milas aq"
]
]
]
let newheaders = [
"authorization": "Token \(userToken!)",
"content-type": "application/json",
"cache-control": "no-cache"
]
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)
print(NSString(data: jsonData, encoding: NSUTF8StringEncoding))
// create post request
let url = NSURL(string: "http://molocate.elasticbeanstalk.com/video/update/")!
let request = NSMutableURLRequest(URL: NSURL(string: "http://molocate.elasticbeanstalk.com/video/update/")!,
cachePolicy: .UseProtocolCachePolicy,
timeoutInterval: 10.0)
request.HTTPMethod = "POST"
request.allHTTPHeaderFields = newheaders
request.HTTPBody = jsonData
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
print(response)
//print(NSString(data: data!, encoding: NSUTF8StringEncoding))
dispatch_async(dispatch_get_main_queue(), {
if error != nil{
print("Error -> \(error)")
return
}
do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
print("Result -> \(result)")
} catch {
print("Error -> \(error)")
}
})
}
task.resume()
} catch {
print(error)
}
} else{
// self.displayAlert("Hata", message: result["result"] as! String)
// UIApplication.sharedApplication().endIgnoringInteractionEvents()
// self.activityIndicator.stopAnimating()
// self.activityIndicator.hidesWhenStopped = true
}
} catch {
print("Error -> \(error)")
}
}
})
dataTask.resume()
do {
try NSFileManager.defaultManager().removeItemAtPath(videoPath!) //.removeItemAtURL(fakeoutputFileURL!)
dispatch_async(dispatch_get_main_queue()) {
print("siiiiil")
self.performSegueWithIdentifier("finishUpdate", sender: self)
}
} catch _ {
}
Your calling the httprequest asynchronously and then trying to use the data on the calling thread. It hasn't been populated until after the http request has returned which will occur at an undetermined future time. Anything you want to do with videoData should be done inside the completion handler, otherwise you are in a race condition and it might be nil when you call it.