title: GET Request Works in Postman but not with RestTemplate - SpringBoot
body: <p>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.</p>
  
  <p>The code i am using -</p>
  
  <pre><code>RestTemplate restTemplate = new RestTemplate();    
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  
  MultiValueMap&lt;String, String&gt; map= new LinkedMultiValueMap&lt;String, String&gt;();
  map.add("client_id", clientid);
  map.add("client_secret",clientsecret);
  
  HttpEntity&lt;?&gt; request = new HttpEntity&lt;&gt;(map, headers);
  logger.info(request.toString());
  
  UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(access_token_url)
                      .queryParam("grant_type", "client_credentials");
  
  logger.info("URI -" + builder.toUriString());
  
  ResponseEntity&lt;String&gt; response = restTemplate.exchange(builder.build().encode().toUri(), 
     HttpMethod.GET, request, String.class);
  logger.info("Response" + response.getBody());
  </code></pre>
  
  <p>The error I am getting is -</p>
  
  <blockquote>
    <p>org.springframework.web.client.HttpClientErrorException$Unauthorized:
    401 Unauthorized</p>
  </blockquote>
  
  <p>But the same works in POSTMAN with the same details.</p>
  
  <p>Something I want to specify</p>
  
  <ul>
  <li>The back-end API is not in my hands.</li>
  <li>The clientid, clientsecret, access_token_url are fetched from properties file and are correct I have cross checked them.</li>
  <li>It is a GET request with x-www-form-urlencoded data. I checked it multiple times it is not POST</li>
  <li>There is no Authorization such as Basic Auth as it is not set in POSTMAN itself</li>
  <li>Went through <a href="https://stackoverflow.com/questions/44285338/httpclienterrorexception-401-unauthorized-basic-authentication">This</a> <a href="https://stackoverflow.com/questions/50240474/spring-resttemplate-gives-401-unauthorized-error-when-sending-with-authorization">This</a> and <a href="https://stackoverflow.com/questions/42741220/spring-resttemplate-receives-401-unauthorized">This</a> nothing helped.</li>
  </ul>
  
  <p>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?</p>
  
  <p>EDIT - 1
  Was marked duplicate with <a href="https://stackoverflow.com/questions/37339753/rest-post-works-correctly-with-postman-but-exception-when-using-spring-resttempl">This</a> but its a different error code overall. The OP got <code>500 Internal Server Error</code> as he was giving wrong content type.</p>
  
  <p>EDIT - 2 
  DEBUG Output-</p>
  
  <pre><code>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
  </code></pre>