I am sending login information to the server that I built using ASP.NET Core. I tested my server via PostMan and Fiddler and it returns a valid response.
Here is my post request:
var request = URLRequest(url: URL(string: "http://adincebic.com/auth/login")!)
request.httpMethod = "POST"
let postString = "email=cebic.ad@gmail.com&password=a"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \. (httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
I tried using dictionary like ["email":"myEmail","password":"myPass"] but it did not let me do it that way. I also tried using Alamofire but again with no luck, the server always returns 415 error in the console.
To verify that my server works I created a UWP application to test it and it worked as expected.
Here is my server response in XCode:
statusCode should be 200, but is 415
response = Optional(<NSHTTPURLResponse: 0x6080002275a0> { URL: http://adincebic.com/auth/login } { status code: 415, headers {
Connection = "keep-alive";
"Content-Length" = 0;
Date = "Thu, 08 Jun 2017 12:16:12 GMT";
Server = "nginx/1.10.1 (Ubuntu)";
} })
responseString = Optional("")
I am assuming that it may be a problem with encoding or a problem with reading data.
I wrote this post requests as described in this answer: HTTP Request in Swift with POST method