Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the jersey client to do a POST operation with request params and a request body

I'm trying to figure how how to use the Jersey client to send both the request params and the request body of a POST operation.

Currently I know how to do it both of those way individually, but not together.

From here: Using the Jersey client to do a POST operation

I've gotten this for the request parms:

MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("name1", "val1");
formData.add("name2", "val2");
ClientResponse response = webResource.type("application/x-www-form-urlencoded").post(ClientResponse.class, formData);

And for the request body I can do the following:

String jsonObject ="... valid json object";
webResource.type(MediaType.APPLICATION_JSON_TYPE).post(String.class, jsonObject);

How do I post both a request param with a request body?

Thanks

like image 341
technocrat Avatar asked Jan 24 '12 19:01

technocrat


People also ask

What is the use of jersey client?

Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities. In this quick tutorial, we will explore the creation of JAX-RS client using Jersey 2. For a discussion on the creation of RESTful Web Services using Jersey, please refer to this article.

What is Jax-RS and Jersey?

The JAX-RS (JSR 311: The Java API for RESTful Web Services) specification provides a standardized Java-based approach to implementing REST-style web services. Jersey is the reference implementation of JAX-RS and I provide a brief introduction to JAX-RS via Jersey in this blog post.


1 Answers

I just figured it out..

webResource.queryParam("key", "value").type(MediaType.APPLICATION_JSON_TYPE).post(String.class, jsonObject);
like image 57
technocrat Avatar answered Oct 01 '22 23:10

technocrat