I'm hitting an API that will occasionally throw a HTTP 403 error, and the response body can give some extra information in the form of json, however for the life of me I can't seem to get the information back out from the Alamofire response objects. I see the information in developer tools if I hit the API via chrome. Here's my code:
Alamofire.request(mutableURLRequest).validate().responseJSON() { (response) in switch response.result { case .Success(let data): if let jsonResult = data as? NSDictionary { completion(jsonResult, error: nil) } else if let jsonArray = data as? NSArray { let jsonResult = ["array" : jsonArray] completion(jsonResult, error: nil) } case .Failure(let error): //error tells me 403 //response.result.data can't be cast to NSDictionary or NSArray like //the successful cases, how do I get the response body? }
I've queried pretty much every object attached to the response, but it doesn't seem to give me the response body back in the case of HTTP errors. Is there a work-around or something I'm missing here?
To read the body of the response, we need to access its Body property first. We can access the Body property of a response using the ioutil. ReadAll() method. This method returns a body and an error.
400 is always "Bad Request" and not "Bad Request - You Forgot To Specify The User ID"). Even if they won't break, they won't care about your special name for specific error code. Show activity on this post. The error does not belong in the body.
The HTTPResponse object contains all outgoing response data. It consists of the HTTP status code and message, the HTTP headers, and any response body data. HTTPResponse also contains the ability to stream or push chunks of response data to the client, and to complete or terminate the request.
It is best practice to include the error message as an entity in the body of the HTTP response - be it JSON, plain text, formatted HTML, or whatever other format you may want to utilize. Show activity on this post. It is better to have error details in the body.
The result meaning of "success" depends on the HTTP method: GET: The resource has been fetched and transmitted in the message body. HEAD: The representation headers are included in the response without any message body. PUT or POST: The resource describing the result of the action is transmitted in the message body.
However, the HTTP Response not only contains the response status code but some other components as well. We will discuss them in this section. The composition of HTTP Response looks like this- Subsequently, let us understand these different components using the same API that we used in the HTTP Request article.
By looking at the various status codes above, we can understand the response from the server. However, the HTTP Response not only contains the response status code but some other components as well. We will discuss them in this section. The composition of HTTP Response looks like this-
I asked this question on their github page and got an answer from cnoon:
swift 2:
if let data = response.data { let json = String(data: data, encoding: NSUTF8StringEncoding) print("Failure Response: \(json)") }
swift 3:
if let data = response.data { let json = String(data: data, encoding: String.Encoding.utf8) print("Failure Response: \(json)") }
https://github.com/Alamofire/Alamofire/issues/1059
I just left out the encoding part, by doing this you are able to get the response json even in the case of errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With