Skip to content

Support for PUTting form-encoded data [SPR-5628] #10299

Closed

Description

@spring-projects-issues

Grzegorz Borkowski opened SPR-5628 and commented

In RESTful services scenario it is not uncommon to accept the incoming data (as service input) in the form-encoded format (Content-type: application/x-www-form-urlencoded). This format is well-known, popular, implemented in every language. See the RESTful Web Service book from O'Reilly for longer discussion of such choice.
Obviously, REST service will accept form-endoded data in POST and PUT request. The problem is that servlet spec (or implementations) seems to be broken in this area. HttpServletRequest.getParameter("foo") method for form-encoded data returns the parameter value only for POST requests, not for PUT. I've tested it on Tomcat and Glassfish.
This also means that all Spring's autobinding features (with command object) will fail for PUT requests. This is really serious problem, as for PUT requests, one have to manually parse and decode the request body.
The solution would be I think to add some filter, which wraps the request when the method is PUT and content type is "application/x-www-form-urlencoded", and override the getParameter() and related methods.
It would be probably also helpful to check the servlet spec for explanation of this behavior, and how it is implemented in Tomcat (or other containers).

To reproduce, simply create servlet or JSP like this:
<%=request.getParameter("foo")%>
and send POST and then PUT request to it, with content-type set to "application/x-www-form-urlencoded" and body equal to "foo=bar"


Affects: 3.0 M3

Issue Links:

Referenced from: commits d746dca, 2cf2fc1

4 votes, 7 watchers

Activity

  1. spring-projects-issues commented on Mar 28, 2009

    @spring-projects-issues
    CollaboratorAuthor
  2. spring-projects-issues commented on May 8, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Grzegorz Borkowski commented

    With Spring 3.0 M3, there is a FormHttpMessageConverter which works with PUT and retrieves all body parameters; so the need for calling getParameter() directly in controller is reduced. This means that this task's priority is much lower now (I have no permission to change it on my own).
    However, I haven't checked how it relates to Spring's autobinding features (with command object).

  3. spring-projects-issues commented on Jun 28, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Mike Yin commented

    I think this is related, but please let me know if it is not, and I can make a new issue. I have a couple of @RequestMapping(value="/blah", method=RequestMethod.PUT) annotated methods. using @RequestParam("someParam") on them works but only if the parameter is in the querystring. if it's in the body, it doesn't get parsed and used as a request parameter, but it does if the method is RequestMethod.POST. Is the only workaround to have a message converter registered that handles x-www-form-urlencoded and use a @RequestBody annotation to have a form parameter map? the reason i need it to be in the body and not the query string is because one of my parameters is a list, and I don't want to hit any URL length issues.

  4. spring-projects-issues commented on Jul 13, 2011

    @spring-projects-issues
    CollaboratorAuthor
  5. spring-projects-issues commented on Jul 25, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    An HttpPutFormContentFilter is available and can be configured if needed. It intercepts HTTP PUT requests where Content-Type:x-www-form-urlencoded, reads the form data from the body of the request, and wraps the ServletRequest to make the form data available as request parameters.

  6. spring-projects-issues commented on Feb 6, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Arjan van B. commented

    See the documentation in Working with "application/x-www-form-urlencoded" data.

    And beware that both HttpPutFormContentFilter (FishEye) and converters such as FormHttpMessageConverter (FishEye) consume the request's input stream. This seems to imply one cannot use HttpPutFormContentFilter in an application that also uses PUT with @RequestBody MultiValueMap<String, String> and/or HttpEntity<MultiValueMap<String, String>>. (Unless the filter is only configured for very specific URLs in <filter-mapping>.)

    Like both of the following will throw IOException: Stream closed when HttpPutFormContentFilter has been configured for use elsewhere in the application:

    @RequestMapping(..., method = RequestMethod.PUT)
    public ... doUpdate(final @RequestBody MultiValueMap<String, String> data, ...) {
      ...
    }
    
    @RequestMapping(..., method = RequestMethod.PUT)
    public ... doUpdate(final HttpEntity<MultiValueMap<String, String>> data, ...) {
      ...
    }
    
  7. spring-projects-issues commented on Feb 8, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    As I mentioned under #13063, feel free to improve the documentation.

  8. spring-projects-issues commented on Feb 19, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Thanks Stevo, that makes sense.

  9. added
    type: enhancement
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions