Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestTemplate - http GET with request body [duplicate]

Tags:

http

get

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

like image 758
Tushar Mishra Avatar asked Nov 07 '12 13:11

Tushar Mishra


1 Answers

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.

like image 83
CodeCaster Avatar answered Oct 19 '22 11:10

CodeCaster