Im trying to implement this API https://api.bnm.gov.my/portal#operation/ERLatest
As per the above URL, its GET request with mandatory Accept herader with value "application/vnd.BNM.API.v1+json"
when i tried with postman, can get response ->
{
"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
}
}
This is what i did to get the same response in my spring boot application ->
@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();
}
But it fails to get the correct response, what im getting is ->
The requested URL was rejected. Please consult with your administrator.
Your support ID is: 10497884431577109860
When i was playing around postman, i have noticed that, when i remove the HOST header i do get this same type of response. But as far as i know, HOST header are automatically set. Is it spring boot RestTemplate will not set this HOST header? if not how to set it manually?
Thank you guys....
solution is to manually set User-Agent header
in your forexExchange() controller method just add this line where you set your headers:
headers.set("User-Agent", "test");
so it looks like this:
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();
}