I my swift app . I want to create a request POST with alamofire
The request
$ curl \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-X POST -d '{ "user": { "email": "[email protected]", "password": "1234" } }' \
http://localhost:3000/users/sign_in
My code
let URL = NSURL(string: "https://lobo-api.herokuapp.com/users/sign_up")!
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.HTTPMethod = "GET"
let parameters = ["user": ["email": "[email protected]", "password": "OPOPO"]]
var JSONSerializationError: NSError? = nil
mutableURLRequest.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &JSONSerializationError)
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(mutableURLRequest).responseJSON { (request, response, data, error) in
println(error)
println(response)
println(data)
println(error)
}
I have got this error
Optional( { URL: https://lobo-api.herokuapp.com/users/sign_up } { status code: 404, headers {
Connection = "keep-alive";
"Content-Length" = 1564;
"Content-Type" = "text/html; charset=utf-8";
Date = "Sun, 05 Apr 2015 19:54:11 GMT";
Server = Cowboy;
Via = "1.1 vegur";
"X-Request-Id" = "12df8182-c91e-439f-b773-2b2e48f3f0c6";
"X-Runtime" = "0.007897";
} })
nil
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x170678500 {NSDebugDescription=Invalid value around character 0.})
First of all in you curl command you use a POST request, but in your code you want to make a GET request.
Then you're getting a 404 response from the server. According with wikipedia exact definition, you have a 404 http status code response which means: "..The 404 or Not Found error message is an HTTP standard response code indicating that the client was able to communicate with a given server, but the server could not find what was requested.." (in your case probably because you must do a POST request instead to a GET)
Finally, you see also a 3840 http status code:
an error that you get each time the server is not responding properly with JSON code and JSON text must be encoded in UTF-8, UTF-16, or UTF-32 (in your case probably because you must do a POST request instead to a GET)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With