简体   繁体   中英

Print Alamofire request body

I'm using Alamofire library for connecting with an API in iOs. I have a problem in one of the connection, and I don't know if it is because of the data encoded in the body or any other thing. In order to detect my error, I'm trying to print in the console the request body for checking if I'm sending the correct data structure.

My code is the following:

func updateUser (#user: User, completionHandler: (responseObject: User?, error: AnyObject?) -> ()) {
    let parameters = [
        "_id": "\(user._id!)",
        "email": "\(user.email!)",
        "media": "\(Mapper().toJSONArray(user.media!))",
        "blogs": "\(Mapper().toJSONArray(user.blogs!))"
    ]

    var manager = Alamofire.Manager.sharedInstance
    manager.request(.PUT, apiUrl + "/route/to/api", parameters: parameters, encoding: .JSON)
        .responseObject{ (req: NSURLRequest, res: NSHTTPURLResponse?, user: User?, data: AnyObject?, error: NSError?) in
            if(error != nil) {
                NSLog("Error API updateUser: \(error)")
            }
            else {
                completionHandler(responseObject: user as User?, error: data)
            }
    }
}

User is a Mappable object, since I'm using ObjectMapper combined with Alamofire. User is defined by the following code:

class User: Mappable {
   var _id: String?
   var name: String?
   var media: [Media]?

   init(_id: String, name: String, media: [Media]){
      self._id = _id;
      self.name = name;
      self.media = media
   }

   required init=(_ map: Map){
      mapping(map)
   }

   func mapping(map: Map){
      _id <- map["_id"]
      name <- map["name"]
      media <- map["media"]
   }
}

Media is defined like User, but with different variables.

Also, I would like to know, in addition of printing request body, if I could include the parameters to Alimofire request in a more efficient way (something like parsing the User object and substituting it for the parameters variable)

Any idea about my problems?

EDIT:

Following the suggestion of @Travis, finally I found the solution for printing the request body. Below you could find the code:

println("request body: \(NSString(data:req.HTTPBody!, encoding:NSUTF8StringEncoding) as String?)")

About passing as parameters an object I couldn't work it, I followed the official documentation, but I could do it.

Swift 3+

print(NSString(data: (response.request?.httpBody)!, encoding: String.Encoding.utf8.rawValue))

The answer to your first question is,

println("request body: \(request.HTTPBody)")

As far as your 2nd question goes, there's a whole section on API Parameter Abstraction as well as CRUD & Authorization on the Alamofire main page .

Added the below extension for the Request class for printing the logs.

extension Request {
    public func debugLog() -> Self {
        #if DEBUG
            debugPrint("=======================================")
            debugPrint(self)
            debugPrint("=======================================")
        #endif
        return self
    }
}

To use the extension, just use debugLog() after defining your request, like so:

Alamofire.request(url).debugLog()
            .responseJSON( completionHandler: { response in
   })

reference url : link

Just to turn it a bit easier.

    if let requestBody = request.request?.HTTPBody {
        do {
            let jsonArray = try NSJSONSerialization.JSONObjectWithData(requestBody, options: [])
            print("Array: \(jsonArray)")
        }
        catch {
            print("Error: \(error)")
        }
    }

斯威夫特 5

print(response.debugDescription)

For Swift 4 & Swift 5, just like that :

String(data: data!, encoding: String.Encoding.utf8)

If not in DefaultDataResponse extension or object, replace data with yourObject.response.data

From Alamofire documentation here https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#curl-command-output

You can get curl request description

AF.request("https://httpbin.org/get")
    .cURLDescription { description in
        print(description)
    }
    .responseDecodable(of: DecodableType.self) { response in
        debugPrint(response.metrics)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM