2016-12-31 70 views
0

我正在使用Alamofire来调用我的API。POST请求不能在swift3中使用alamofire

以下是代码。

func alamofirePost() { 
let headers: HTTPHeaders = [ "content-type": "x-www-form-urlencoded"] 
let todosEndpoint: String = "http://54.244.108.186:4000/api/post_upc" 
let newTodo: [String: Any] = ["UPC": codeTextView.text, "DATE_TIME":   currentTime() ] 
print("i am in alamo") 
Alamofire.request(todosEndpoint, method: .post, parameters: newTodo ,encoding: JSONEncoding.default,headers: headers) 
.responseJSON { response in 
guard response.result.error == nil else { 
// got an error in getting the data, need to handle it 
print("error calling POST on /todos/1") 
print(response) 
print(response.result.error!) 
return 
} 

当我调用的函数,它试图在数据库

INSERT INTO `UPC`(`UPC`,`DATE_TIME`) VALUES (NULL,NULL) 

下面插入空值是响应,当我在邮递员应用进行操作。

enter image description here

能有人帮

回答

1

首先在你的邮差要求你要发送你的身体x-www-form-urlencoded但在你的斯威夫特例如你指定标题为好。但实际上,您将自己的POST正文提交为JSON有效内容。相反,您的邮递员请求是一组键/值对。

此外,这两个键似乎与您的Swift示例和Postman示例名称不同。

雨燕采用UPCDATE_TIME而邮递员upc_appdttm_app这样至少你要确保你沿着你的API预计

+0

大科迪发送。谢谢。我研究并得到了解决方案。 – Uyyan