Md. Najmul Hasan Md. Najmul Hasan - 20 days ago 10
JSON Question

How to read json data of a bad request (status code 400) using Alamofire

I was calling a Rest API locally from terminal. This was like below:

http -v -f --timeout=60 GET 'http://localhost:8080/api/v1/public/users/signin?'

It is returning following output:

GET /api/v1/public/users/signin? HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: localhost:8080
User-Agent: HTTPie/0.8.0

HTTP/1.1 400 Bad Request
Content-Type: application/json
Date: Mon, 28 Mar 2016 16:59:08 GMT
Set-Cookie: rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Sun, 27- Mar-2016 16:59:13 GMT
Transfer-Encoding: chunked

"debugMessage": "Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken -, rememberMe=true] did not match the expected credentials.Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken -, rememberMe=true] did not match the expected credentials.: The subject was expected to be true, but was false",
"logId": "50a695c1a81e0542",
"stackTrace": "org.niopack.r.....

Now I want to read this JSON data using Alamofire. I tried with responseJSON serialiser with following way:

Alamofire.request(method, urlString, parameters: parameters, encoding: .URL, headers: cookieHeader)
.responseJSON {

alamofireResponse in

let jsonData = alamofireResponse.result.value

But this I am getting jsonData as nil. What you will do if you were are here?



For me removing the validate() allowed me to see the reason the request was failing. For some reason when validate fails, the data of the response is not parsed.