title: Spring RestTemplate - http GET with request body
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/978061/http-get-with-request-body">HTTP GET with request body</a>  </p>
  </blockquote>
  
  
  
  <p>I've read few discussions here which do not advocate sending content via HTTP GET. There are restrictions on the size of data that can be sent via clients (web browsers). And handling GET data also depends on servers. Please refer section <strong>Resources</strong>  below. </p>
  
  <p>However, I've been asked to test the possibility to send content via HTTP GET using RestTemplate. I refered few discussions on spring forum but they were not answered. (Please note sending data via http Post works fine). The discussion <a href="https://stackoverflow.com/questions/11091160/rest-api-get-request-with-body">here</a> suggests using POST instead.</p>
  
  <p>dev env - JBoss AS 5.1, Spring 3.1.3</p>
  
  <p><strong>Client</strong> </p>
  
  <pre><code>    @Test
  public void testGetWithBody()
  {
      // acceptable media type
      List&lt;MediaType&gt; acceptableMediaTypes = new ArrayList&lt;MediaType&gt;();
      acceptableMediaTypes.add(MediaType.TEXT_PLAIN);
  
      // header
      HttpHeaders headers = new HttpHeaders();
      headers.setAccept(acceptableMediaTypes);
  
      // body
      String body = "hello world";
      HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(body, headers);
  
      Map&lt;String, Object&gt; uriVariables = new HashMap&lt;String, Object&gt;();
      uriVariables.put("id", "testFile");
  
      // Send the request as GET
      ResponseEntity&lt;String&gt; result = restTemplate.exchange(
              "http://localhost:8080/WebApp/test/{id}/body",
              HttpMethod.GET, entity, String.class, uriVariables);
  
      Assert.assertNotNull(result.getBody());
  }
  </code></pre>
  
  <p><strong>Server</strong> @Controller</p>
  
  <pre><code>    @RequestMapping(value = "/{id}/body", method = RequestMethod.GET)
  public @ResponseBody
  String testGetWithBody(@PathVariable String id,
          @RequestBody String bodyContent)
  {
      return id + bodyContent;
  }
  </code></pre>
  
  <p><strong>The problem</strong> - 
  executing this test case returns    500 Internal Server Error. On debugging, I found that the controller is not hit.</p>
  
  <ol>
  <li><p>Is it correct to understand that the RestTemplate provides the way to send data as request body, but the error occurs because the server could not handle the request body ?</p></li>
  <li><p>If the request body sent via HTTP Get is not conventional why does RestTemplate provide the APIs to allow sending it ? Does this mean there are few servers capable of handling the Request body via GET ? </p></li>
  </ol>
  
  <blockquote>
    <p><strong>Resources</strong> - discussions on sending body via HTTP GET using RestTemplate at spring forum</p>
  </blockquote>
  
  <p><a href="http://forum.springsource.org/showthread.php?129510-Message-body-with-HTTP-GET&amp;highlight=resttemplate+http+get" rel="noreferrer">http://forum.springsource.org/showthread.php?129510-Message-body-with-HTTP-GET&amp;highlight=resttemplate+http+get</a></p>
  
  <p><a href="http://forum.springsource.org/showthread.php?94201-GET-method-on-RestTemplate-exchange-with-a-Body&amp;highlight=resttemplate+http+get" rel="noreferrer">http://forum.springsource.org/showthread.php?94201-GET-method-on-RestTemplate-exchange-with-a-Body&amp;highlight=resttemplate+http+get</a></p>
  
  <blockquote>
    <p><strong>Resources</strong> - General discussions on sending body via HTTP GET</p>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/questions/978061/http-get-with-request-body">get-with-request-body</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/5216567/is-this-statement-correct-http-get-method-always-has-no-message-body">is-this-statement-correct-http-get-method-always-has-no-message-body</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/6031136/get-or-post-when-reading-request-body">get-or-post-when-reading-request-body</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/266322/http-uri-get-limit">http-uri-get-limit</a> </p>