Alamofire: Parse JSON error in Alamofire 3.0

Created on 1 Dec 2015  路  5Comments  路  Source: Alamofire/Alamofire

I'm migrating code from Alamofire 2.0 and I cannot find an easy way to parse the JSON response when the status code is different from 2xx (we put an error message in the base key as rails convention).
On AF 2.0 the NSData with the response was available in the result .Failure, but now it seems that just the NSError is passed in this case.
What's the easiest way to get the body of the response?

support

Most helpful comment

the problem is that response.result.value is now nil on Alamofire 3, so the problem is not parsing, is actually finding the body data somewhere.

All 5 comments

You can use the SwiftyJSON. Very easy and very useful.
https://github.com/SwiftyJSON/SwiftyJSON

the problem is that response.result.value is now nil on Alamofire 3, so the problem is not parsing, is actually finding the body data somewhere.

The only solution I can see is, that you shouldn't use responseJSON method. You can use the method listed below and parse NSData to JSON using NSJSONSerialization or SwiftyJSON.

public func response(
        queue queue: dispatch_queue_t? = nil,
        completionHandler: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Void)
        -> Self

Ok, I wonder if this new behaviour was really thought throughly: the trade-off between performance and the ability of correctly consuming JSON APIs should (imho) be strongly in the favour of the second one.

Okay, let me clear up some things here. @Neku, the data is available in the response object. Therefore, it is always available, if it was returned from the server, as response.data.

@tomekh7, you can absolutely use responseJSON with SwiftyJSON. You can initialize a JSON object from NSData or from an AnyObject that has already been parsed by NSJSONSerialization.

If the future, these types of questions should be opened as a question on Stack Overflow. We use GitHub for bug reports and feature request.

Cheers. 馃嵒

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tib picture tib  路  3Comments

Tulleb picture Tulleb  路  3Comments

dpstart picture dpstart  路  3Comments

hengchengfei picture hengchengfei  路  3Comments

sarbogast picture sarbogast  路  3Comments