title: GET request in RestTemplate in JSON
body: <p>It's been troubling for a couple days already for a seemingly super straightforward question:</p>
  
  <p>I'm making a simple GET request using RestTemplate in <code>application/json</code>, but I keep getting</p>
  
  <pre><code>org.springframework.web.client.HttpClientErrorException: 400 Bad Request
      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
      at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:636)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:592)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:552)
      at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:470)
  </code></pre>
  
  <p>I did research and followed this <a href="http://docs.spring.io/autorepo/docs/spring-android/1.0.x/reference/html/rest-template.html" rel="nofollow noreferrer">tutorial</a>, also I looked at solutions from this <a href="https://stackoverflow.com/questions/4075991/post-request-via-resttemplate-in-json/4077502#4077502">POST request via RestTemplate in JSON</a>. But none of them helped, here's my code:</p>
  
  <pre><code>RestTemplate restTemplate = new RestTemplate();    
  HttpHeaders requestHeaders = new HttpHeaders();    
  requestHeaders.setContentType(MediaType.APPLICATION_JSON);
  HttpEntity&lt;?&gt; requestEntity = new HttpEntity&lt;Object&gt;(requestHeaders);
  restTemplate.exchange(endpoint, HttpMethod.GET, requestEntity, String.class);
  </code></pre>
  
  <p><code>endpoint</code> is <code>http://localhost:8080/api/v1/items?itemIds=" + URLEncoder.encode(itemIds, "UTF-8")</code> which works fine in Postman.
  <code>itemIds</code> is a comma-separated list like below:</p>
  
  <p><code>5400028914,5400029138,5400029138,5400029138,5400029138,5400028401,5400028918,5400028076,5400028726</code></p>
  
  <p>I also tried to use <code>getForObject</code> like below:</p>
  
  <pre><code>String result = restTemplate.getForObject(endpoint, String.class);
  </code></pre>
  
  <p>which gives me this error:</p>
  
  <pre><code>org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
  </code></pre>
  
  <p>I'm not sure what I missed or did wrong, but the same endpoint works perfectly on Postman, but the only difference is that I added Content-Type header in Postman app.</p>
  
  <p>This is my Request from Postman:</p>
  
  <p><code>
  GET /api/v1/items?itemIds=abc%2cdef%2cghi HTTP/1.1
  Host: localhost:8080
  Connection: keep-alive
  Postman-Token: 84790e06-86aa-fa8a-1047-238d6c931a68
  Cache-Control: no-cache
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
  Content-Type: application/json
  Accept: */*
  Accept-Encoding: gzip, deflate, sdch
  Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
  </code></p>
  
  <p>So how can I correctly set content-type using RestTemplate if my above code is wrong?</p>
  
  <p>Another deep dive, I've fired up <a href="https://www.wireshark.org/" rel="nofollow noreferrer">Wireshark</a> to capture the two HTTP requests, here are the screenshots:</p>
  
  <p>The request from Postman: <a href="https://i.stack.imgur.com/Spe1k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Spe1k.png" alt="request from Postman"></a></p>
  
  <p>The request from my Java code: <a href="https://i.stack.imgur.com/2w8Zo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2w8Zo.jpg" alt="request from Java program"></a></p>
  
  <p>I still don't see why the one from my Java program throws 400 while the one from Postman works fine.</p>
  
  <p>Thanks a lot.</p>