18

Possible Duplicate:
HTTP GET with request body

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 Resources below.

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 here suggests using POST instead.

dev env - JBoss AS 5.1, Spring 3.1.3

Client

    @Test
public void testGetWithBody()
{
    // acceptable media type
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    acceptableMediaTypes.add(MediaType.TEXT_PLAIN);

    // header
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(acceptableMediaTypes);

    // body
    String body = "hello world";
    HttpEntity<String> entity = new HttpEntity<String>(body, headers);

    Map<String, Object> uriVariables = new HashMap<String, Object>();
    uriVariables.put("id", "testFile");

    // Send the request as GET
    ResponseEntity<String> result = restTemplate.exchange(
            "http://localhost:8080/WebApp/test/{id}/body",
            HttpMethod.GET, entity, String.class, uriVariables);

    Assert.assertNotNull(result.getBody());
}

Server @Controller

    @RequestMapping(value = "/{id}/body", method = RequestMethod.GET)
public @ResponseBody
String testGetWithBody(@PathVariable String id,
        @RequestBody String bodyContent)
{
    return id + bodyContent;
}

The problem - executing this test case returns 500 Internal Server Error. On debugging, I found that the controller is not hit.

  1. 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 ?

  2. 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 ?

Resources - discussions on sending body via HTTP GET using RestTemplate at spring forum

http://forum.springsource.org/showthread.php?129510-Message-body-with-HTTP-GET&highlight=resttemplate+http+get

http://forum.springsource.org/showthread.php?94201-GET-method-on-RestTemplate-exchange-with-a-Body&highlight=resttemplate+http+get

Resources - General discussions on sending body via HTTP GET

get-with-request-body

is-this-statement-correct-http-get-method-always-has-no-message-body

get-or-post-when-reading-request-body

http-uri-get-limit

Community
  • 1
  • 1
Tushar Mishra
  • 1,370
  • 1
  • 12
  • 20
  • 1
    as is not allowed post answers in questions marked as duplicate i will make it here cause spent huge amount of time to figure out how to do it You can make RestTemplate make GET requests with body by using it over apache http client with some tuning: – LifeIsGoodMF Mar 29 '17 at 12:23
  • class HttpGetWithBody extends HttpEntityEnclosingRequestBase { HttpGetWithBody(final URI uri) { setURI(uri); } @Override public String getMethod() { return HttpGet.METHOD_NAME; } } – LifeIsGoodMF Mar 29 '17 at 12:23
  • HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(){ @Override protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) { if (httpMethod == HttpMethod.GET) { return new HttpGetWithBody(uri); } return super.createHttpUriRequest(httpMethod, uri); } }; – LifeIsGoodMF Mar 29 '17 at 12:25
  • RestTemplate restTemplate = new RestTemplate(requestFactory); – LifeIsGoodMF Mar 29 '17 at 12:25
  • A detailed description of the good solution that @LifeIsGoodMF has given above in the comments you find [here](https://mekaso.rocks/get-requests-with-a-request-body-spring-resttemplate-vs-apache-httpclient). – Foofighter2146 Aug 29 '18 at 13:38

1 Answers1

7

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 ?

You can tell by looking at network traffic (does the request get sent with a request body and a GET method?) and at server logs (the 500 result you receive must have a server-side effect that gets logged, and if not, configure the server to do so).

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 ?

Because it is a generic class that also allows you to craft requests that can include a message body.

As stated in HTTP GET with request body:

In other words, any HTTP request message is allowed to contain a message body, and thus [a server] must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

A body on a GET cannot do anything semantically, because you are requesting a resource. It's like you tell the server: "Give me resource X, oh, and have some apples!". The server won't care about your apples and happily serve resource X - or throw an error because it doesn't like any offers in a request.

However, I've been asked to test the possibility to send content via HTTP GET

Please tell the one who requested this that this is a case that should not have to be tested, because no sensible implementation supports it.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thanks CodeCaster for your quick response ! Although, POST seems to be the method to be implemented, I'll configure the server to check logs. – Tushar Mishra Nov 07 '12 at 14:14
  • 6
    Sometimes it's more convenient to have the query parameters implemented as a json object and put in the body rather than the url. In which case the server is interested in the apples. – nevster May 11 '16 at 05:56
  • (Which is not implying it's necessarily a good idea.) – nevster May 11 '16 at 06:08