我有一个接收到String响应的unirest请求,但无法解析其结果。我进行了一项研究,但所看到的只是关于如何解析json请求的教程,这显然不是我所需要的。无论如何,请求看起来像这样:
public static String getToken() {
//HttpResponse<String> response = null;
try {
//Unirest request
HttpResponse<String> response = Unirest.post("http://api.nuvelco.com/token")
.header("content-type", "application/x-www-form-urlencoded")
.header("cache-control", "no-cache")
.body("grant_type=password&username=demo_user&password=demo_pass&client_id=paymentApp")
.asString();
//attempts I took to print a proper response on the logs
logger.info("RAW BODY: " + response.getRawBody());
logger.info("BODY: " + response.getBody());
logger.info("STATUS: " + response.getStatus());
return null;
} catch (UnirestException e) {
e.printStackTrace();
return null;
}
}
响应应该看起来像这样:
{
"access_token": "sample_access_token",
"token_type": "bearer",
"expires_in": 3599,
"refresh_token": "sample_refresher_token",
"as:client_id": "paymentApp",
"username": "demo_user",
".issued": "Thu, 08 Jun 2017 06:19:50 GMT",
".expires": "Thu, 08 Jun 2017 07:19:50 GMT"
}
但是以某种方式,我总是会收到以下答复:
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.13.4</center>
</body>
</html>
我不确定它是否相关,但是我正在使用JavaScript来调用Java函数getToken()
。任何帮助将非常感激。谢谢。
答案 0 :(得分:0)
是否不添加Accept: application/json
来请求标题帮助?
答案 1 :(得分:0)
尝试将/
添加到Unirest.post("http://api.nuvelco.com/token")
中的URL。
像这样:
Unirest.post("http://api.nuvelco.com/token/")