【问题标题】:Alamofire post body format is not correctAlamofire 帖子正文格式不正确
【发布时间】:2017-12-05 12:20:44
【问题描述】:

我使用 Alamofire 调用 api 方法。我是这样用的

Alamofire.request("some api",
        method:method,
        parameters:body,
        encoding:URLEncoding.default,
        headers: finalHeaders)
        .responseJSON { (apiResponse) in
        ...

body 是 Strings 和 Any 的字典。当我发送一个帖子请求时,服务器会得到这样的内容:

key1=StringValue&key2=intValue

虽然我需要我的身体格式是这样的:

"key1":"StringValue","key2":intValue

什么参数没有设置或设置错误?

【问题讨论】:

    标签: ios iphone swift alamofire


    【解决方案1】:

    将内容类型设置为 JSON。

    _ = ["Content-Type": "application/json"]
    

    检查这个答案 Alamofire Accept and Content-Type JSON

    【讨论】:

    • 这不是答案,但帮助我找到了答案
    【解决方案2】:

    答案是这样改变编码类型

    Alamofire.request("some api",
            method:method,
            parameters:body,
            encoding:JSONEncoding.default,
            headers: finalHeaders)
            .responseJSON { (apiResponse) in
            ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2016-08-26
      相关资源
      最近更新 更多