users@jersey.java.net

Re: [Jersey] PostReplaceFilter does not get parameters from body

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 15 Dec 2008 12:24:12 +0100

On Dec 12, 2008, at 3:35 AM, Ferdy Nagy wrote:

> Hi,
>
> I'm using a PostReplaceFilter so that a caller can GET a resource
> via a POST operation, which works fine; however parameters sent in
> the body with content-type: application/x-www-form-encoded are not
> available, they're always empty in the parameter map. It works if
> the parameters are included in the query string.
>

When you say "parameter map" what are you referring to? Can you share
some code?


> I want to be able to pass a potentially long set of parameters when
> GETing the resouce and they need to be encoded in the body. Looking
> through the list it seems that this might be caused by the filter
> reading the stream before the parameters are parsed from the body as
> Jersey reads the stream to build up the parameter set not the
> ServletRequest?
>
> Is there anyway I can change the PostReplaceFilter so that the
> parameters in the body are parsed into the UriInfo object? I tried
> adding a @Consumes to the @Get method but that does not seem to
> work, and I didn't really want to do that anyway as the @Get should
> in theory consume, or not, anything.
>

It seems as if you want to do something very specific and convert form
parameters of a POSTed request entity into query parameters of the
request URI, is that correct?

If so you need to write your own filter to do that as
PostReplaceFilter is only meant to work on the HTTP method and is not
designed to modify the request entity or the request URI.

You can obtain the form parameters from

   ContainerRequest.getFormParameters()

then you can iterate through the form parameters and build a new URI:

   UriBuilder ub = cr.getRequestUriBuilder();
     // Iterate over form parameters:
         ub.queryParam(..., ....);

Then set the new URI of the request:

   cr.setUris(cr.getBaseUri(), ub.build());


Do you no about @FormParam? why don't you use that with an explicit
POST request sent from the client?

Paul.