users@jersey.java.net

Re: [Jersey] Jersey Client usage for POSTing to REST endpoints

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 18 Aug 2008 23:14:12 +0200

On Aug 18, 2008, at 7:07 PM, Arul Dhesiaseelan wrote:

> Hello,
>
> I recently wrote a blog entry (http://aruld.info/yahoo-search-
> restful-client-using-jersey/) on using Jersey Client for invoking
> Yahoo Search APIs.
>
> Is there a better way to construct query params on the client side
> when doing a POST using Jersey Client? I am not sure if my current
> approach shown below is correct. But it works.
>
> Context params can be sometimes lengthy, and the below construction
> may look ugly in that case.
>
> WebResource r = client.resource("http://search.yahooapis.com/
> WebSearchService/V1/contextSearch");
> ResultSet searchResults = r.accept(MediaType.APPLICATION_XML).post
> (ResultSet.class, "appid=YahooDemo&query=madonna&context=Italian
> +sculptors+and+painters+of+the+renaissance+favored+the+Virgin+Mary
> +for+inspiration");
>

What you are sending are not query parameters but form parameters
encoded in the body of the request.

You can use the class:

   com.sun.jersey.api.representation.Form

as follows:

   Form f = new Form();
   f.add("appid", "YahooDemo");
   ...
   ResultSet searchResults = r.accept(MediaType.APPLICATION_XML).post
(ResultSet.class, f);

If using query parameters then you can use UriBuilder to construct
the URI and add query parameters.

Paul.

> Appreciate any suggestions.
>
> -Arul
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>