When i run my code i get this error and i don\'t know why.
Error Domain=NSCocoaErrorDomain Code=3840 \"No value.\" UserInfo={NSDebugDescription=No value.}
I
You need to set the content-type header value to use JSON.
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
Updated the code to Swift 3 and removed everything unrelated to the request:
let myUrl = URL(string: "http://foodhelper.club/registerUser.php");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let postString = "userEmail=email&userFirstName=firstname&userLastName=lastname&userPassword=password";
request.httpBody = postString.data(using: String.Encoding.utf8);
URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in
if error != nil {
print("fail")
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
print ("1")
if let parseJSON = json {
let userId = parseJSON["userId"] as? String
print ("2")
if( userId != nil) {
} else {
let errorMessage = parseJSON["message"] as? String
print ("3")
}
}
} catch{
print(error)
}
}).resume()