我正在尝试在Swift中运行HTTP请求,将Dictionary中的2个参数发送到URL。
电子邮件& API_KEY
如何在Post方法中传递词典??
这段代码给我错误..
结果 - > [“status”:{“status_code”= 3;“status_text”=“InvalidAccess。”;}}
func postValidateDataInAPI(){
let json: [String: Any] = ["email": "ravi.bokade@yudiz.com", "api_key": " "]
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
// create post request
let url = URL(string: "http://www.eyeon.events/eyeon/rest/preregister_validate.php")!
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
// insert json data to the request
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
if error != nil{
print("Error -> \(error)")
return
}
do {
let result = try JSONSerialization.jsonObject(with: data!, options: []) as? [String:Any]
print("Result -> \(result!)")
} catch {
print("Error -> \(error)")
}
}
task.resume()
} catch {
print(error)
}
}