403 when using spring boot, but works well with postman

Viewed 2580

Im making api call with postman on url:

https://cex.io/api/order_book/BTC/USD

plain GET no headers no params no nothing. But the same with java:

RestTemplate rt = new RestTemplate();
rt.getForObject("https://cex.io/api/order_book/BTC/USD", String.class);

gets me 403. where is the problem?

1 Answers

RestTemplate sets "User-Agent: Java_version" header, and it seems the site you are trying to query denies access with that user-agent.

You can explicitly set a user-agent instead of the default one like:

    HttpHeaders headers = new HttpHeaders();
    headers.set("User-agent", "SomeUserAgent");
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    RestTemplate rt = new RestTemplate();
    String result = rt.exchange("https://cex.io/api/order_book/BTC/USD", HttpMethod.GET, entity, String.class).getBody();
Related