Hi I'm new to swift and I made following api call o send data but it's not sending and I got following response.
Sent data
firstName=gggg&lastName=ggg&username=fgg&password=ghh&email=ggg@gg.com&latitude=25.0710693470323&longitude=55.143004052641
response
responseString {"status":"error","message":"Oops!!!Something went wrong..."}
But I can get all other validation messages such as "username cannot be empty".
But I tried with Postman It also gives same error message as above on header methods but later I figured out and sent on Body Method and form of application/x-www-form-urlencoded then I got success response as below.
Following is the My API Call... Please somebody figure out what I am doing wrong or suggest me a better post api call.
One more thing this is the same API call method I made it for "/homefeed" and got the response but we don't need to send any specific parameters for that. Please help me.
func addNewUser()
{
let url = URL(string: "https://xxxxxxxxxx.azurewebsites.net/api/addUser")!
let firstName:String = firstnameTextFeild.text!
let lastName:String = lastNameTxtField.text!
let username:String = usernameTextField.text!
let password:String = passwordTextField.text!
let email:String = emailTextField.text!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "firstName=\(firstName)&lastName=\(lastName)&username=\(username)&password=\(password)&email=\(email)&latitude=\(lat)&longitude=\(long)"
print("Sent Data -",postString)
request.httpBody = postString.data(using: .utf8)
/*
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
} catch let error {
print(error.localizedDescription)
}
*/
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))")
print("responseString ", responseString ?? "resSt")
}
task.resume()
}