休息 API 在 postman 中工作,但不在 spring boot 中工作
rest API working in postman but not in spring boot
我正在尝试实现这个 API https://api.bnm.gov.my/portal#operation/ERLatest
根据上述 URL,它的 GET 请求带有强制性的 Accept herader,值为 "application/vnd.BNM.API.v1+json"
当我尝试使用 postman 时,可以得到响应 ->
{
"data": [
{
"currency_code": "AUD",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 2.7454,
"selling_rate": 2.7904,
"middle_rate": null
}
},
{
"currency_code": "CAD",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 3.0465,
"selling_rate": 3.0915,
"middle_rate": null
}
},
{
"currency_code": "EUR",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 4.7336,
"selling_rate": 4.7786,
"middle_rate": null
}
},
{
"currency_code": "GBP",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 5.3769,
"selling_rate": 5.4269,
"middle_rate": null
}
},
{
"currency_code": "JPY",
"unit": 100,
"rate": {
"date": "2020-05-04",
"buying_rate": 4.0464,
"selling_rate": 4.0914,
"middle_rate": null
}
},
{
"currency_code": "SGD",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 3.0368,
"selling_rate": 3.0788,
"middle_rate": null
}
},
{
"currency_code": "USD",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 4.33,
"selling_rate": 4.355,
"middle_rate": null
}
}
],
"meta": {
"quote": "rm",
"session": "1130",
"last_updated": "2020-05-04 12:16:13",
"total_result": 7
}
}
这就是我在 spring 引导应用程序中获得相同响应的方法 ->
@RequestMapping(value="/forex_check")
public String forexExchange() throws Exception{
String url="https://api.bnm.gov.my/public/exchange-rate/USD";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/vnd.BNM.API.v1+json");
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,entity,String.class);
return response.getBody();
}
但是没有得到正确的响应,我得到的是 ->
The requested URL was rejected. Please consult with your administrator.
Your support ID is: 10497884431577109860
当我和邮递员打交道时,我注意到,当我删除 HOST header 时,我确实得到了同样类型的响应。但据我所知,HOST header 是自动设置的。难道spring开机RestTemplate不会设置这个HOSTheader?如果不是如何手动设置?
谢谢大家....
解决方法是手动设置User-Agentheader
在您的 forexExchange() 控制器方法中,只需在您设置 header 的位置添加此行:
headers.set("User-Agent", "test");
所以看起来像这样:
public String forexExchange() throws Exception{
String url="https://api.bnm.gov.my/public/exchange-rate/USD";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/vnd.BNM.API.v1+json");
headers.set("User-Agent", "test");
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,entity,String.class);
return response.getBody();
}
我正在尝试实现这个 API https://api.bnm.gov.my/portal#operation/ERLatest
根据上述 URL,它的 GET 请求带有强制性的 Accept herader,值为 "application/vnd.BNM.API.v1+json"
当我尝试使用 postman 时,可以得到响应 ->
{
"data": [
{
"currency_code": "AUD",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 2.7454,
"selling_rate": 2.7904,
"middle_rate": null
}
},
{
"currency_code": "CAD",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 3.0465,
"selling_rate": 3.0915,
"middle_rate": null
}
},
{
"currency_code": "EUR",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 4.7336,
"selling_rate": 4.7786,
"middle_rate": null
}
},
{
"currency_code": "GBP",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 5.3769,
"selling_rate": 5.4269,
"middle_rate": null
}
},
{
"currency_code": "JPY",
"unit": 100,
"rate": {
"date": "2020-05-04",
"buying_rate": 4.0464,
"selling_rate": 4.0914,
"middle_rate": null
}
},
{
"currency_code": "SGD",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 3.0368,
"selling_rate": 3.0788,
"middle_rate": null
}
},
{
"currency_code": "USD",
"unit": 1,
"rate": {
"date": "2020-05-04",
"buying_rate": 4.33,
"selling_rate": 4.355,
"middle_rate": null
}
}
],
"meta": {
"quote": "rm",
"session": "1130",
"last_updated": "2020-05-04 12:16:13",
"total_result": 7
}
}
这就是我在 spring 引导应用程序中获得相同响应的方法 ->
@RequestMapping(value="/forex_check")
public String forexExchange() throws Exception{
String url="https://api.bnm.gov.my/public/exchange-rate/USD";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/vnd.BNM.API.v1+json");
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,entity,String.class);
return response.getBody();
}
但是没有得到正确的响应,我得到的是 ->
The requested URL was rejected. Please consult with your administrator.
Your support ID is: 10497884431577109860
当我和邮递员打交道时,我注意到,当我删除 HOST header 时,我确实得到了同样类型的响应。但据我所知,HOST header 是自动设置的。难道spring开机RestTemplate不会设置这个HOSTheader?如果不是如何手动设置?
谢谢大家....
解决方法是手动设置User-Agentheader
在您的 forexExchange() 控制器方法中,只需在您设置 header 的位置添加此行:
headers.set("User-Agent", "test");
所以看起来像这样:
public String forexExchange() throws Exception{
String url="https://api.bnm.gov.my/public/exchange-rate/USD";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/vnd.BNM.API.v1+json");
headers.set("User-Agent", "test");
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,entity,String.class);
return response.getBody();
}