【问题标题】:Setting content-type header to use JSON with Swift 3 + AlamoFire将内容类型标头设置为将 JSON 与 Swift 3 + AlamoFire 一起使用
【发布时间】:2017-12-02 11:07:30
【问题描述】:

Alamofire Swift 3.0 Extra parameter in call 中的答案对我不起作用。

将标头设置为nil 可以编译,但我需要["Content-Type", "application/json"]。这里我得到一个方法中的额外参数的错误

如何服用

manager.request(url, method: .get, parameters: parameters).responseJSON { response in fulfill(response) } }

并发送 JSON 内容类型?

文档显示

Automatic Validation

Automatically validates status code within 200..<300 range, and that the Content-Type header of the response matches the Accept header of the request, if one is provided.

Alamofire.request("https://httpbin.org/get").validate().responseJSON { response in
    switch response.result {
    case .success:
        print("Validation Successful")
    case .failure(let error):
        print(error)
    }
}

我正在使用.responseJSON,但我没有返回 JSON。所以我想我需要发送Content-Type 标头。

【问题讨论】:

    标签: ios json swift3 alamofire


    【解决方案1】:

    试试这个,还有另一个方法重载允许传递带有标题的字典

        let request = Alamofire.request(requestUrl, method: .get, parameters: [:], encoding: URLEncoding.queryString, headers: ["Content-Type" :"application/json"]).responseData { (response) in
            /***YOUR CODE***/
        }
    

    对于在请求中发布 JSON 数据,请检查此答案 Using manager.request with POST

    希望对你有帮助

    【讨论】:

    • @quantumpotato 这个答案对你有帮助吗?你的问题解决了吗?
    猜你喜欢
    • 2011-08-12
    • 1970-01-01
    • 2017-02-03
    • 2017-06-16
    • 1970-01-01
    • 2015-04-18
    • 2020-02-22
    • 2015-06-07
    • 2014-04-16
    相关资源
    最近更新 更多