Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestTemplate getForObject getting 404

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?

like image 989
Antonio682 Avatar asked Feb 06 '26 12:02

Antonio682


1 Answers

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.

like image 116
mohsen Avatar answered Feb 08 '26 02:02

mohsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!