Announcement Announcement Module
Collapse
No announcement yet.
Message body with HTTP GET Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Message body with HTTP GET

    All,

    I've been wrestling with Spring Rest 3.1, trying to send an HTTP message body as part of a GET request. I don't seem to have any difficulty conveying such a body via POST, but it's not working for GET.

    I apologize for not having the code handy to post herein. I will post it when I get to work tomorrow.

    Here are the basics:

    postforentity works
    getforentity doesn't
    getforobject doesn't
    exchange doesn't

    I am serializing from object to json (and back) via Jackson. This seems to work well in the postforentity case.

    For the moment (until i can post the code), can anyone tell me if conveying a message body via GET is supported? I have seen conflicting reports on this including these two remarks from Fielding (emphasis mine):

    In general, most applications that involve user-supplied data being supplied to the server are not safe. The only exception is when the information is in the form of generalized query parameters, for which there is a trade-off between GET and POST that usually involves the size of the parameter content. GET is only desirable for those cases where the parameters can be expressed as a meaningful URI.

    Yes. In other words, any HTTP request message is allowed to contain a message body, and thus 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. So, yes, you can send a body with GET, and no, it is never useful to do so.

    Thank you for your help.

    -Saul

  • #2
    Code:
    As promised, here is the failing code from my Spring Rest client:
    
    	private ResponseEntity<NsbMsg> requestGetService(String svcUrl, NsbMsg nsbMsg)     
    	{
    		System.out.println(svcUrl + "::" + nsbMsg.diagTag);
    		RestTemplate myRestTemplate = new RestTemplate();
    		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    		acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
    		HttpHeaders headers = new HttpHeaders();
    		headers.setAccept(acceptableMediaTypes);
                    headers.setContentType(MediaType.APPLICATION_JSON);
    		HttpEntity<NsbMsg> request = new HttpEntity<NsbMsg>(nsbMsg, headers);
    		ResponseEntity<NsbMsg> response = myRestTemplate.exchange(svcUrl, HttpMethod.GET, request, NsbMsg.class);		return response;
    	}
    It fails as follows at the call to exchange():

    Code:
    Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:90)
    	at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:494)
    	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:451)
    	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409)
    	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:385)
    	at com.company.spring.rest.NsbRestClient.requestGetService(NsbRestClient.java:137)
    What is "bad" about this request? I am stumped and would be most grateful for some help. Thanks.

    -Saul

    Comment


    • #3
      Anyone?

      Should I cross-post to the "Web" board?

      -Saul

      Comment

      Working...
      X