I have a request that is working fine when I try to send through postman. I was trying to implement the same using code where I faced an error.
The code i am using -
RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>(); map.add("client_id", clientid); map.add("client_secret",clientsecret); HttpEntity<?> request = new HttpEntity<>(map, headers); logger.info(request.toString()); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(access_token_url) .queryParam("grant_type", "client_credentials"); logger.info("URI -" + builder.toUriString()); ResponseEntity<String> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, request, String.class); logger.info("Response" + response.getBody()); The error I am getting is -
org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized
But the same works in POSTMAN with the same details.
Something I want to specify
- The back-end API is not in my hands.
- The clientid, clientsecret, access_token_url are fetched from properties file and are correct I have cross checked them.
- It is a GET request with x-www-form-urlencoded data. I checked it multiple times it is not POST
- There is no Authorization such as Basic Auth as it is not set in POSTMAN itself
- Went through This This and This nothing helped.
My main concern is I have never seen anywhere sending a GET request with method body, but it is working in POSTMAN and if it is working there why not in the code? Where I am going wrong?
EDIT - 1 Was marked duplicate with This but its a different error code overall. The OP got 500 Internal Server Error as he was giving wrong content type.
EDIT - 2 DEBUG Output-
2019-07-31 14:53:05.208 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.client.RestTemplate : HTTP GET ORIGINAL_URL?grant_type=client_credentials 2019-07-31 14:53:05.215 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.client.RestTemplate : Accept=[text/plain, application/json, application/*+json, */*] 2019-07-31 14:53:05.218 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.client.RestTemplate : Writing [{client_id=[CLIENTID], client_secret=[CLIENT_SECRET]}] as "application/x-www-form-urlencoded" 2019-07-31 14:53:06.976 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.client.RestTemplate : Response 401 UNAUTHORIZED 2019-07-31 14:53:07.034 DEBUG 3576 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json] 2019-07-31 14:53:07.035 DEBUG 3576 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Nothing to write: null body 2019-07-31 14:53:07.039 DEBUG 3576 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK 2019-07-31 14:53:07.108 DEBUG 3576 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : GET "/favicon.ico", parameters={} 2019-07-31 14:53:07.119 DEBUG 3576 --- [io-8080-exec-10] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []] 2019-07-31 14:53:07.181 DEBUG 3576 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : Completed 200 OK
curl -X GET \ 'URL-OF-CLIENT?grant_type=client_credentials' \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'client_secret=SECRET&client_id=ID'