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?

3
  • try with postman or ARC first against the endpoint and then check with the code Commented Aug 8, 2017 at 14:21
  • As I said in the previous post in Postman works perfectly that's the weird for me. Thanks for your response Commented Aug 8, 2017 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. Commented Aug 8, 2017 at 14:31

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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();

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.