How to get the response data out of the NSHTTPURLResponse in the callback of AFJSONRequestOperation?
Asked Answered
H

3

34

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?

Helga answered 16/5, 2012 at 22:46 Comment(0)
G
52

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.

Gavan answered 22/5, 2012 at 3:49 Comment(5)
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?Helga
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.Gavan
I'm referring specifically to the method JSONRequestOperationWithRequest:success:failure:Helga
Within 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.Helga
what about the new AFHTTPSessionManager? You don't get the operation object therePhionna
H
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)
}
Hertford answered 28/6, 2019 at 5:11 Comment(0)
C
-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.

Ceyx answered 9/5, 2013 at 11:11 Comment(1)
JSON object may be nil in failure callback block, as it might be failing due to improper json.Feil

© 2022 - 2024 — McMap. All rights reserved.