ios - 调试 Alamofire 请求

标签 ios json swift alamofire

我正在尝试调试由 Alamofire 发送到服务器端点的 HTTP header 和正文。当我从正在处理的端点收到确认时,所有 header 参数似乎都是正确的,但是,我遇到了服务器问题,我在参数字段中发送给它的 JSON 没有被解析.我如何找出请求中发送的内容?


let headers: HTTPHeaders = [
                    "API-Key": apiKey,
                    "Accept": "application/json",
                    "Content-Type": "application/json"
                ]

let data = ["name":"Don Jonhson"]


Alamofire.request(endPointUrl, method: .post,  parameters: data, 
encoding: URLEncoding.httpBody, headers: headers).responseJSON() { 
           response in

           if let status = response.response?.statusCode {
              switch(status){
               case 201:
                debugPrint(response)
               default:
                debugPrint(response)
              }
           }

          ...

最佳答案

您正在使用一个简单的字符串字典,您必须改用 alamofire 的参数

let params: Parameters = ["name":"Don Jonhson"]

Alamofire.request(endPointUrl, method: .post,  parameters: params, 
encoding: URLEncoding.httpBody, headers: headers).responseJSON() { 
       response in

       if let status = response.response?.statusCode {
          switch(status){
           case 201:
            debugPrint(response)
           default:
            debugPrint(response)
          }
       }

      ...

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

相关文章:

ios - 无法在 UITableViewCell 中呈现

ios - Storyboard连接检查器中的方法名称发生更改

ios - MPMoviePlayerController 如何应对 HTTP 404 和 504 错误?

ios - 如何在 CoreMotion 框架中找到最小和最大频率间隔

objective-c - 使用未声明的标识符

c# - 将 json 字符串反序列化为对象 - Silverlight

c# - JSON.net 直接从 oledbconnection 序列化

c# - 统一解析嵌套的json

ios - 滑动以查看 UITableViewCells 崩溃时的操作

ios - 如何将 UInt32 转换为 UInt8