我的POST有问题,它会返回500作为回复。
这是我的代码:
let postParams: Dictionary<String, AnyObject> = ["user_id": user_id, "prayer": [["user_id":data["user_id"], "prayer_date":data["prayer_date"], "name":data["name"]]]]
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(postParams, options: NSJSONWritingOptions())
} catch {
print("bad things happened")
}
print(request.HTTPBody)
// Make the POST call and handle it in a completion handler
session.dataTaskWithRequest(request, completionHandler: { ( data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
// Make sure we get an OK response
guard let realResponse = response as? NSHTTPURLResponse where realResponse.statusCode == 200 else {
print("Not a 200 response")
print(response)
return
}
// Read the JSON
if let postString = NSString(data:data! , encoding: NSUTF8StringEncoding) as? String {
// Print what we got from the call
let dictionary = self.JSONParseDictionary(postString)
let status = (dictionary as NSDictionary)["status"] as! Bool
print(status)
}
}).resume()
从API开始,它需要user_id
作为int
和prayer
作为数组。