3

I'm trying to make a get request to an Api Rest but I'm always getting 404, nevertheless if try copying queryUrl in a browser or postMan it works perfectly.

restTemplate.getForObject(queryUrl, entity ,Integer.class);

I've also tried this:

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(httpHeaders);
log.debug("request headers: " + entity.getHeaders());
ResponseEntity response = restTemplate.exchange(queryUrl, 
HttpMethod.GET, entity, String.class);

But nothing changes. Can anyone help me?

Luca
  • 1,766
  • 3
  • 27
  • 38
Antonio682
  • 363
  • 1
  • 4
  • 18
  • try with postman or ARC first against the endpoint and then check with the code – Barath Aug 08 '17 at 14:21
  • As I said in the previous post in Postman works perfectly that's the weird for me. Thanks for your response – Antonio682 Aug 08 '17 at 14:26
  • Without much information we can't do anything .. your usage is right. Might be u can share some url just to ensure everything is right or share the working postman one sample screenshot would do. – Barath Aug 08 '17 at 14:31

4 Answers4

5

This took me a bit to figure out, so I'll leave a little note here hoping one day it helps someone. I kept getting a 404 when making a get request with ResTemplate.getForEntity(URL, Type). The URL worked in Chrome, Postman, but not in my code. It ended up being because I had an unsafe character in my URL, an % to be exact, because my URL had spaces and so instead of space the URL had %20. Postman and Chrome could handle this, but not my code. Once I replaced the unsafe character with an actual space all was well.

Hope it works out for you too.

mohsen
  • 1,686
  • 3
  • 15
  • 15
0

You were right Barath the usage is correct. The problem was that UriComponentBuilder was including a blank space at the en of the URL.

I've fixed it with a trim.

String queryUrl = UriComponentsBuilder.fromUriString(HOST)
                .path(PATH)
                .buildAndExpand(param)
                .toUriString().trim();
Antonio682
  • 363
  • 1
  • 4
  • 18
0

When your url contains "%", then you shoud use restTemplate.getForObject(new URI(url),xx.class), it worked for me.

yiyun yang
  • 11
  • 1
0

I was also facing the same issue for me the issue was not the encoded path but another application (nodeJS) was running on the same port.

kumartj
  • 33
  • 5