3

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?

Juan Carlos Mendoza
  • 5,558
  • 7
  • 24
  • 50
filemonczyk
  • 1,433
  • 2
  • 22
  • 42

1 Answers1

8

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();
helospark
  • 1,363
  • 6
  • 11