ios - Alamofire - 错误的请求

标签 ios swift alamofire postman objectmapper

恐怕我无法分享 API 网址。但我检查了 postman , 有用。这是一个 发布 请求和以下是响应:

{
    "user_key": "b0aebdedb15e2beaaf479ca3c4f8227e8e970681"
}  

postman 截图:

enter image description here

在代码中,这是我使用 Alamofire 发出的请求:
Alamofire.request("some url", method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: ["Content-Type":"application/json"])
            .responseObject { (response: DataResponse<User>) in

                let userResponse = response.result.value
                print("")
    }

但是 userResponse 为零。这是用户 目的 :
import ObjectMapper

class User: Mappable {

    var authToken: String?

     required init?(map: Map) {

    }

    func mapping(map: Map) {
        self.authToken <- map["user_key"]
    }


}

我正在使用 ObjectMapper这里。

假设它不是 JSON对象,我试图将其作为 String 处理:
 Alamofire.request("some url", method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: ["Content-Type":"application/json"])
            .responseString { (response: DataResponse<String>) in
                let dataUser = response.data as! Any
                let dataUser1 = response.data as! AnyObject
                let error = response.error
                let code = response.response?.statusCode
                print(response.value)
                print(response.result.value)
        }  

依然没有。我无法检索该值。然而,状态码是 400(错误请求)。
它到底是什么? JSON对象或 String ?我该如何解决 ?

最佳答案

只是尝试更换

这个URLEncoding.httpBodyJSONEncoding.default
JSONEncoding.default

用途 JSONSerialization创建参数对象的 JSON 表示,该对象设置为请求的主体。 Content-Type编码请求的 HTTP header 字段设置为 application/json .

Alamofire reference

关于ios - Alamofire - 错误的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52160240/

相关文章:

objective-c - 在 Objective-C 中子类化时更改变量的类

ios - Swift UIButton 子类化并根据变量改变颜色

ios - 让图像 if nil 在 swift 3 中消失(或缩小,使其看起来消失)

ios - Realm :按子对象中的属性排序

swift - 如何使用alamofire在ios中传递访问 token

ios - 测试外部附件框架应用程序

ios - 我应该为 UTTypeImage 导入什么库,它是 iOS 15 中 kUTTypeImage 的替代品?

ios - Alamofire:遵循 HTTP 重定向(或不遵循)

ios - 如何限制for循环中的网络调用次数?

ios - 如何在 swift ios 的单个 uitableviewcell 中显示多个字典或数组数据