【问题标题】:Debugging Alamofire request调试 Alamofire 请求
【发布时间】:2017-04-06 18:23:13
【问题描述】:

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

          ...

【问题讨论】:

    标签: ios json swift alamofire


    【解决方案1】:

    你正在使用一个简单的字符串字典,你必须使用 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)
              }
           }
    
          ...
    

    【讨论】:

    • 谢谢。如何检查名称是否已作为 JSON 正确插入到 http 正文中?
    • 当您收到回复时,您可以检查它是否为 200,以便您收到一些数据,否则您可以检查您的后端以查看您发送给他们的用户是否已创建。你试过我的答案,如果它有效,你能把它标记为正确答案吗?
    • 我已经收到了 200 个回复。问题是我从服务器收到一个错误,说请求正文中的 JSON 不正确。这就是我要调试的。服务器端点正在正确解释标头信息,即 API-Key、内容类型等。服务器还应该解析包含一些 JSON 数据的 http 正文。服务器端点抱怨发送的 JSON。所以我想弄清楚的是,当请求离开 iPhone 时 http 正文中的实际内容以及如何从 Alamofire 获取该信息?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多