35

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);
Stine
  • 1,535
  • 5
  • 20
  • 43

5 Answers5

29

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)
Alden
  • 6,293
  • 2
  • 32
  • 46
  • I tried that but got a `MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json,...`? – Stine Dec 14 '13 at 20:32
  • Well that's an entirely different problem... you should either register the default JacksonFeature, or write your own MessageBodyWriter/Reader – Alden Dec 14 '13 at 21:07
  • Ah I see you wrote that above, sorry. So `null` is the correct answer, right? – Alden Dec 14 '13 at 21:08
  • 9
    Thats stupid. Should be an Enity.empty() static method – Shervin Asgari Mar 18 '15 at 12:29
  • 2
    The problem with this approach is that jersey will still set the content-type header according to the Entity-factory (in this case `application/json`). – Hank Jul 16 '15 at 08:44
  • This works but I believe it does not send a 'Content-Length: 0' header... some endpoints apparently require a Content-Length if you send a POST. Is there a way to add a header manually? – Kevin Hooke Dec 09 '15 at 21:52
  • You can add headers between the request and post calls if you need to: `.request(APPLICATION_JSON).header(HttpHeaders.CONTENT_LENGTH, 0).post(Entity.json(null), MyClass.class)` – Alden Apr 07 '16 at 12:08
  • As @Hank points out this will make a request with the `Content-Type` set to `application/json` which might not be what you want. If you want to POST an empty form, try `post(Entity.form(new Form()))` – michiakig Mar 20 '17 at 17:52
  • It doesn't work for me. Entity.json("") works fine. – K. Gol Sep 03 '19 at 09:40
10

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.

Scott Perham
  • 2,125
  • 1
  • 7
  • 18
Gapmeister66
  • 705
  • 7
  • 13
8

I don't know if the version change it. But, the following doesn't work:

builder.put( Entity.json( null ) );

Where, the following works fine:

builder.put( Entity.json( "" ) );

James Oravec
  • 16,761
  • 25
  • 77
  • 145
roanbaga
  • 81
  • 1
  • 3
  • This worked for me. I was trying to unit test where the parameter value comes through as `null`. I tried the `Entity.json("{}")` answer suggested but that gave me a parameter with an instance with every field `null` rather than the parameter which is what I wanted. – Chris R Nov 14 '19 at 13:07
3

Just post an empty txt.

   .post(Entity.text(""));
Emmanuel Osimosu
  • 3,966
  • 1
  • 30
  • 35
0

It worked for me only with an empty json object string

.post(Entity.json("{}")

All other solutions, still produced 400 Bad Request

P.S. The request is done using MediaType.APPLICATION_JSON

svarog
  • 8,471
  • 4
  • 55
  • 66
Michael
  • 1,270
  • 2
  • 6
  • 10