Post empty body with Jersey 2 client
Asked Answered
N

5

38

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);
Nozzle answered 14/12, 2013 at 16:5 Comment(1)
I'm late to the party, but FWIW your update is the answer to my particular problem - specifically I don't want a Content-Type header :)Goaltender
B
30

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)
Boroughenglish answered 14/12, 2013 at 19:35 Comment(9)
I tried that but got a MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json,...?Nozzle
Well that's an entirely different problem... you should either register the default JacksonFeature, or write your own MessageBodyWriter/ReaderBoroughenglish
Ah I see you wrote that above, sorry. So null is the correct answer, right?Boroughenglish
Thats stupid. Should be an Enity.empty() static methodAbysm
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).Galbanum
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?Outreach
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)Boroughenglish
As @Galbanum 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()))Jessy
It doesn't work for me. Entity.json("") works fine.Byrdie
C
13

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.

Chalcography answered 15/9, 2016 at 21:43 Comment(0)
T
9

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

Trenttrento answered 25/6, 2016 at 23:36 Comment(1)
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.Myrna
A
4

Just post an empty txt.

   .post(Entity.text(""));
Aden answered 9/5, 2017 at 11:45 Comment(0)
S
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

Sensibility answered 22/3, 2018 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.