【问题标题】:Alamofire POST RequestAlamofire POST 请求
【发布时间】:2017-02-10 18:13:54
【问题描述】:

我希望有人可以解释 POST 请求(带参数)完成了什么。当我运行 Alamofire 文档提供的以下命令时,会生成以下命令:

代码

let url = "https://httpbin.org/post"
let parameters: Parameters = [
        "foo": "bar",
        "baz": ["a", 1],
        "qux": [
            "x": 1,
            "y": 2,
            "z": 3
        ]
    ]


Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
    if((response.result.value) != nil) {
        let jsonVar: JSON = JSON(response.result.value!)
        print(jsonVar)
    }
}

结果

{
"files" : {

},
"origin" : "192.104.181.247",
"data" : "{\"baz\":[\"a\",1],\"qux\" {\"y\":2,\"x\":1,\"z\":3},\"foo\":\"bar\"}",
"headers" : {
"Accept" : "*\/*",
"Accept-Encoding" : "gzip;q=1.0, compress;q=0.5",
"Content-Type" : "application\/json",
"Host" : "httpbin.org",
"Content-Length" : "53",
"User-Agent" : "HTTPRequest\/1.0 (com.dexstrum.HTTPRequest; build:1; iOS 10.2.0) Alamofire\/4.3.0",
"Accept-Language" : "en;q=1.0"
},
"json" : {
"baz" : [
  "a",
  1
],
"foo" : "bar",
"qux" : {
  "x" : 1,
  "y" : 2,
  "z" : 3
}
},
"form" : {

},
"args" : {

},
"url" : "https:\/\/httpbin.org\/post"
}

参数和 POST 请求到底发生了什么?

【问题讨论】:

    标签: json post swift3 alamofire


    【解决方案1】:

    您点击的 url 允许 post 请求。您可以通过 getpost 方法获得 json 响应。通常你通过 get 方法获取数据并通过 post 方法发送数据。当使用 post 请求发送数据时,它需要一些 parameter 来满足其要求,通常是其预定义的。

    所以您在这里发送 post 请求,其中包含 alamofire 以及一些满足其要求的 参数。在这里,您将 parameter 编码为 JSONEncoding.default 这意味着它最终将编码为 jsonparameter 数据发送给您得到响应为 json 因为你声明了 .responseJSON 。希望对您有所帮助。

    【讨论】:

    • 是的,确实有帮助。我意识到我的参数可能传递错误或构造错误。
    • 我想知道为什么我创建的参数没有正确传递。我让参数:Parameters = [“x”:2,“y”:2],这是我的邮政编码Alamofire.request(url,方法:.post,参数:参数,编码:JSONEncoding.default).responseJSON { if((response.result.value) != nil) { let jsonVar: JSON = JSON(response.result.value!) print(jsonVar) } else { print(response.result.value ?? "no response ") } }
    • 它正在产生无响应结果。
    • 你确定你点击的 url 接受 "x" : 2, "y" : 2 这些参数??如果我的回答对您有帮助,请接受答案:)
    猜你喜欢
    • 2018-07-27
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2016-11-25
    • 2020-02-10
    • 1970-01-01
    相关资源
    最近更新 更多