59

In a Java method, I'd like to use a Jersey client object to do a POST operation on a RESTful web service (also written using Jersey) but am not sure how to use the client to send the values that will be used as FormParam's on the server. I'm able to send query params just fine.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jon Onstott
  • 13,499
  • 16
  • 80
  • 133

6 Answers6

87

Not done this yet myself, but a quick bit of Google-Fu reveals a tech tip on blogs.oracle.com with examples of exactly what you ask for.

Example taken from the blog post:

MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("name1", "val1");
formData.add("name2", "val2");
ClientResponse response = webResource
    .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
    .post(ClientResponse.class, formData);

That any help?

brabster
  • 42,504
  • 27
  • 146
  • 186
53

Starting from Jersey 2.x, the MultivaluedMapImpl class is replaced by MultivaluedHashMap. You can use it to add form data and send it to the server:

    WebTarget webTarget = client.target("http://www.example.com/some/resource");
    MultivaluedMap<String, String> formData = new MultivaluedHashMap<String, String>();
    formData.add("key1", "value1");
    formData.add("key2", "value2");
    Response response = webTarget.request().post(Entity.form(formData));

Note that the form entity is sent in the format of "application/x-www-form-urlencoded".

tonga
  • 11,749
  • 25
  • 75
  • 96
  • Do I still need to `URLEncoder.encode(value1, "UTF-8")` the value or will `form()` take care of the correct encoding internally? – user1438038 May 10 '21 at 14:33
18

It is now the first example in the Jersey Client documentation

Example 5.1. POST request with form parameters

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:9998").path("resource");

Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");

MyJAXBBean bean =
target.request(MediaType.APPLICATION_JSON_TYPE)
    .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
        MyJAXBBean.class);
Olivier Tonglet
  • 3,312
  • 24
  • 40
  • Does Jersey client automatically convert the return type into `MyJAXBBean.class` ? How to achieve this? – macemers Jun 30 '15 at 02:58
  • 1
    The last parameter of the call chain is a class telling Jersey to map the response content to a MyJAXBBean object. You can also map it to a string and deserialize it with a tool of your own chosing. – Olivier Tonglet Jun 30 '15 at 15:30
  • 1
    This is the link if someone's still looking - https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/client.html#client.ex.formpost – Sankalp Sep 24 '19 at 11:39
6

If you need to do a file upload, you'll need to use MediaType.MULTIPART_FORM_DATA_TYPE. Looks like MultivaluedMap cannot be used with that so here's a solution with FormDataMultiPart.

InputStream stream = getClass().getClassLoader().getResourceAsStream(fileNameToUpload);

FormDataMultiPart part = new FormDataMultiPart();
part.field("String_key", "String_value");
part.field("fileToUpload", stream, MediaType.TEXT_PLAIN_TYPE);
String response = WebResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
dimuthu
  • 865
  • 11
  • 15
  • If you're not posting a text file, you probably want to use `MediaType.APPLICATION_OCTET_STREAM_TYPE` for the stream... – Lambart Feb 04 '22 at 21:21
3

Simplest:

Form form = new Form();
form.add("id", "1");    
form.add("name", "supercobra");
ClientResponse response = webResource
  .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
  .post(ClientResponse.class, form);
supercobra
  • 15,810
  • 9
  • 45
  • 51
2

Also you can try this:

MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("name1", "val1");
formData.add("name2", "val2");
webResource.path("yourJerseysPathPost").queryParams(formData).post();
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
RVelarde
  • 29
  • 1