I'm new to Swift. I want to Authenticate a user. I'm using 'Deployd' as my server. This is what API's documentation says:
HTTP
To authenticate a user, send a POST request to /login with username and password properties in the request body.
POST /users/login
{ "username": "johnsmith", "password": "password" }
I'm using Alamofire to parse JSON data. Here is my code:
let user = "root"
let password = "root"
// this is a testing user i've created in deployd's dashboard.
let credential = NSURLCredential(user: user, password: password, persistence: .ForSession)
Alamofire.request(.POST, "http://localhost:2406/users/login/\(user)/\(password)")
.authenticate(usingCredential: credential)
.response { request, response, _, error in
println(response)
}
This is the response from Xcode console:
Optional(<NSHTTPURLResponse: 0x7fdd906dc3a0> { URL: http://localhost:2406/users/login/root/root } { status code: 400, headers {
Connection = "keep-alive";
"Content-Type" = "application/json";
Date = "Thu, 06 Aug 2015 13:01:54 GMT";
"Transfer-Encoding" = Identity; } })
I know I'm doing it in total wrong manner.
You are using http authentication (headers), but server / api is not asking you for that (it actually says 400 - bad request - so that should point out to wrong usage of the API);
Instead, you have to provide parameters in the request body because specification says that it should be what you are providing to server. To do that, use different method of Alamofire that allows you to include parameters:
let parameters = ("username" : user, "password" : password)
Alamofire.request(.POST, "http://localhost:2406/users/login", parameters: parameters)
And remove .authentication from the call altogether :)
Hope it helps!