can not get request body with content-type=application/x-www-form-urlencoded

Viewed 8883

I can not get request body contents when request contains Content-Type=application/x-www-form-urlencoded.

I have following method for retrieving request body:

@RequestMapping(value = "/testRequestBody", method = RequestMethod.POST)
@ResponseBody
public String testRequestBody(@RequestBody String requestBody) {
    return requestBody;
}

I am using restclient firefox plugin to test request. When I add Content-Type=application/x-www-form-urlencoded to request requestBody variable becomes empty despite I have text in the request body. When I remove Content-Type header, request body is populated with text.

UPD This issue seems to be related to tomcat which is used to run spring container. When application/x-www-form-urlencoded request comes, it is parsed, parameters defined in the body become request parameters and body then is cleared.

I could access body parameters in the following manner:

@RequestMapping(value = "/testRequestBody", method = RequestMethod.POST)
@ResponseBody
public String testRequestBody(@RequestParam("paramName") String paramName) {
    return requestParam;
}

How can I configure tomcat not to parse request and not to clear request body?

1 Answers

When you invoke a RESTful SAS BI Web service, the HTTP headers that you send are important. The Content-type HTTP header tells SAS BI Web Services what type of content you are sending when you perform an HTTP POST. If you are invoking a JSON endpoint, then your Content-type must be set as application/x-www-form-urlencoded and your content must be encoded using this format. If you are invoking an XML endpoint, then your Content-type must be set to application/xml and your content must be XML. The HTTP Accept header tells SAS BI Web Services what type of content your client can accept as output. The Accept header must be set to application/json for JSON endpoints and can be set to application/xml for XML endpoints (this header is not required for XML endpoints)

@RequestMapping(value = "/testRequestBody", method = RequestMethod.POST, headers="Accept=*")
@ResponseBody
public String testRequestBody(@RequestBody String requestBody) {
    return requestBody;
}
Related