mark mark - 7 months ago 140
Swift Question

Printing data in Alamofire request

I am trying to print the data returned from an alamofire post request in swift like so:

Alamofire.request(.POST, "http://blablabla.com/test1", parameters: ["operand1": "123"]).response { request, response, data, error in
print(request)
print(response)
print(data, radix: 16)
print(error)
}


but when I try to print data it doesn't print a string a string, instead it prints:
<796570>

I am a bit new to swift so I don't really know if there is a way or a reason for this.

Answer

That is the raw server response which is in NSData type. You can make use of some built-in serializers such as .responseString() in order to convert that into user readable value:

Alamofire.request(.POST, "http://blablabla.com/test1", parameters: ["operand1": "123"]).responseString { response in
  print("Response String: \(response.result.value)")
}

You can check out other serializers in the Alamofire documentation.