如何使用 Alamofire 读取错误请求(状态代码 400)的 json 数据

Md.*_*san 3 json alamofire

我从终端本地调用 Rest API。就像下面这样:

http -v -f --timeout=60 GET 'http://localhost:8080/api/v1/public/users/signin?email=myemail@email.com&password=mypassword'
Run Code Online (Sandbox Code Playgroud)

它返回以下输出:

GET /api/v1/public/users/signin?email=myemail@email.com&password=mypassword 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 - najmul@qianalysis.com, rememberMe=true] did not match the expected credentials.Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - najmul@qianalysis.com, rememberMe=true] did not match the expected credentials.: The subject was expected to be true, but was false",
    "errorCode": "INCORRECT_CREDENTIALS",
    "logId": "50a695c1a81e0542",
    "stackTrace": "org.niopack.r.....
}
Run Code Online (Sandbox Code Playgroud)

现在我想使用 Alamofire 读取此 JSON 数据。我尝试通过以下方式使用responseJSON序列化器:

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

        alamofireResponse in

        let jsonData = alamofireResponse.result.value
    }
Run Code Online (Sandbox Code Playgroud)

但这我得到的 jsonData 为零。如果你在这里你会做什么?

谢谢。

Sha*_*yag 5

对我来说,删除validate()允许我看到请求失败的原因。由于某种原因validate失败时,不会解析响应的数据。