Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post empty body with Jersey 2 client

How do I submit a post request with an empty body with a Jersey 2 client?

final MyClass result = ClientBuilder.newClient()     .target("http://localhost:8080")     .path("path")     .queryParam("key", "value")     .request(APPLICATION_JSON)     .post(What to fill in here if the body should be left empty??, MyClass.class); 

Update: this works:

final MyClass result = ClientBuilder     .newBuilder().register(JacksonFeature).build()     .target("http://localhost:8080")     .path("path")     .queryParam("key", "value")     .request(APPLICATION_JSON)     .post(null, MyClass.class); 
like image 950
Stine Avatar asked Dec 14 '13 16:12

Stine


People also ask

What is jersey client used for?

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.

What is Jersey Apache client?

Jersey is a REST-client, featuring full JAX-RS implementation, neat fluent API and a powerfull filter stack. Apache Http Client is a HTTP-client, perfect in managing low-level details like timeouts, complex proxy routes and connection polling. They act on a different levels of your protocol stack.

What is Jersey client API?

Jersey ClientBuilder. JAX-RS Client API is a designed to allow fluent programming model. To create jersey client follow these steps – Use ClientBuilder. newClient() static method.


2 Answers

I can't find this in the doc's anywhere, but I believe you can use null to get an empty body:

final MyClass result = ClientBuilder.newClient()     .target("http://localhost:8080")     .path("path")     .queryParam("key", "value")     .request(APPLICATION_JSON)     .post(Entity.json(null), MyClass.class) 
like image 165
Alden Avatar answered Sep 22 '22 10:09

Alden


I found that this worked for me:

Response r = client     .target(url)     .path(path)     .queryParam(name, value)     .request()     .put(Entity.json("")); 

Pass an empty string, not a null value.

like image 26
Gapmeister66 Avatar answered Sep 23 '22 10:09

Gapmeister66