Post empty body with Jersey 2 client

Stine picture Stine · Dec 14, 2013 · Viewed 32.8k times · Source

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);

Answer

Alden picture Alden · Dec 14, 2013

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)