调试Alamofire请求

时间:2017-04-06 18:23:13

标签: ios json swift alamofire

我正在尝试调试HTTP标头&由Alamofire发送到服务器端点的主体。所有的头参数似乎都是正确的,因为我得到了我正在处理的端点的确认,但是,我遇到了服务器的问题,我在参数字段中发送给它的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)
              }
           }

          ...

1 个答案:

答案 0 :(得分:2)

您正在使用简单的字典字典,而您必须使用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)
          }
       }

      ...