users@jersey.java.net

Re: [Jersey] URL encoding of a question mark to start a query parameter string

From: James Adams <monocongo_at_gmail.com>
Date: Thu, 20 Aug 2009 15:39:31 -0500 (CDT)

The answer is to instead use queryParam() or queryParams() instead of
constructing the query string within the path() method.

In other words replace the below code with this:

            return webResource.path("physical_hosts")
                              .queryParam("fqdn", fqdn)
                              .accept(DEFAULT_MEDIA_TYPE)
                              .header(HTTP_AUTHORIZATION_HEADER,
authorizationHeader)
                              .header(CLIENT_SPECIFICATION_HEADER,
CLIENT_SPECIFICATION_VERSION)
                              .get(PhysicalHostModel.class);

Thanks to Craig McClanahan for the help!

--James


James Adams wrote:
>
> I'm having trouble getting a question mark to encode correctly (or to go
> through unencoded, not sure what really I need to do here), resulting in a
> 404 even though the resource is actually available.
>
> I have client code which looks like this:
>
> try
> {
> return webResource.path("physical_hosts" +
> URLEncoder.encode("?", "UTF-8") + "fqdn=" + fqdn)
> .accept(DEFAULT_MEDIA_TYPE)
> .header(HTTP_AUTHORIZATION_HEADER,
> authorizationHeader)
> .header(CLIENT_SPECIFICATION_HEADER,
> CLIENT_SPECIFICATION_VERSION)
> .get(PhysicalHostModel.class);
> }
> catch (UniformInterfaceException ex)
> {
> throw ErrorsHelper.asException(ex.getResponse());
> }
>
> It always results in a UniformInterfaceException which tells me this:
>
> com.sun.jersey.api.client.UniformInterfaceException: GET
> http://localhost:8080/myapp/resources/physical_hosts%3Ffqdn=xyz.abc.com
> returned a response status of 404
>
> I get the same result if don't use the encode() method on the question
> mark, so it appears that I'm already getting URL encoding by default.
>
> However I can get to the resource without error via a browser GET if I use
> the URL with the question mark intact:
>
> http://myhost.abc.com:8080/myapp/resources/physical_hosts?fqdn=xyz.abc.com
>
> Do I want to do the encoding as I have above (or let the
> WebResource.path() method do it for me, as it appears to be when I don't
> use URLEncoder.encode())? If not then how can I suppress the encoding so
> that the question mark stays intact in the URL?
>
> Thanks in advance for any suggestions, insight, etc.
>
> --James
>

-- 
View this message in context: http://n2.nabble.com/URL-encoding-of-a-question-mark-to-start-a-query-parameter-string-tp3480197p3481586.html
Sent from the Jersey mailing list archive at Nabble.com.