【问题标题】:POST request not working in swift3 with alamofirePOST 请求在 swift3 中无法使用 alamofire
【发布时间】:2016-12-31 19:20:17
【问题描述】:

我正在使用 Alamofire 调用我的 API。

下面是代码。

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)

以下是我在邮递员应用程序中的响应。

有人可以帮忙吗

【问题讨论】:

    标签: swift post alamofire


    【解决方案1】:

    首先在您的邮递员请求中,您将您的正文发布为x-www-form-urlencoded,但在您的 Swift 示例中,您也指定了该标头。但是您实际上是将您的 POST 正文作为 JSON 有效负载提交。相反,您的 Postman 请求是一组键/值对。

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

    Swift 使用 UPC 和 DATE_TIME,而 Postman 使用 upc_app 和 dttm_app,因此您至少要确保发送 API 所期望的内容

    【讨论】:

    • 伟大的科迪。谢谢。我四处研究并找到了解决方案。
    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 2019-06-27
    • 2017-04-02
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    相关资源
    最近更新 更多