I have a situation where I need to access the raw response data for an AFJSONRequestOperation, from within the callback block which includes only NSHTTPURLResponse. I am able to get the statusCode from the NSHTTPURLResponse, but don't see any way to get to the raw data. Is there a good way that anyone knows of to access this from the failure callback block of this operation?
How to get the response data out of the NSHTTPURLResponse in the callback of AFJSONRequestOperation?
Asked
Active
Viewed 3.7k times
3 Answers
51
NSHTTPURLResponse
only contains HTTP header information; no body data. So no, this would be impossible. If you have any control over this code, have the block or method pass the operation itself and get responseData
or responseJSON
.

mattt
- 19,544
- 7
- 73
- 84
-
Matt, that is what I thought, and I did end up referring to the operation to get at the responseData, but it sure feels clumsy that way. Have you considered adding an alternative method in addition to the present one that returns responseData as well NSHTTPURLResponse? – Todd Hopkinson May 22 '12 at 20:08
-
What method are you talking about? All of the AFN methods that have callbacks return everything that you could need to know about the state of the operations--namely the operation object itself, along with a few other salient parts. – mattt May 22 '12 at 22:34
-
I'm referring specifically to the method JSONRequestOperationWithRequest:success:failure: – Todd Hopkinson May 23 '12 at 17:00
-
7Within the failure block I'm able to refer to the NSURLResponse 'response', but as you previously mentioned, it only contains the HTTP header info, no body data. But in certain cases, even failures, I need (and I think others will need) to see the actual responseData that was returned. I know of no other way to get to it but through the 'operation' object. So, what I'm wondering is whether or not you've thought about adding a parameter in the return block for responseData... perhaps this is just too edge case for this though. – Todd Hopkinson May 23 '12 at 17:06
-
5what about the new AFHTTPSessionManager? You don't get the operation object there – Agos Nov 01 '13 at 21:29
3
In the callback from responseJSON
you can get the body from a DataResponse<Any>
object using response.result.value
or String(data:response.data!, encoding: .utf8)
.
You can't get it from response.response
, which is a NSHTTPURLResponse
object. This only has header info.
Alamofire.request(URL(string: "https://foo.com")!).validate(statusCode: 200..<300).responseJSON() { response in
let body = response.result.value
}
If the http response doesn't validate the above doesn't work. but this does:
Alamofire.request(URL(string: "https://foo.com")!).validate(statusCode: 200..<300).responseJSON() { response in
let body = String(data:response.data!, encoding: .utf8)
}

spnkr
- 952
- 9
- 18
-4
Old question, but anyway...
You don't need to get to the operation object, you can easily do something like:
NSData * data = [NSJSONSerialization dataWithJSONObject:JSON options:0 error:nil]];
With the JSON id that the callback receives.
-
JSON object may be nil in failure callback block, as it might be failing due to improper json. – Syed Absar Sep 27 '15 at 08:17