Reputation: 1665
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);
Upvotes: 39
Views: 43963
Reputation: 3256
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
Upvotes: 0
Reputation: 91
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( "" ) );
Upvotes: 9
Reputation: 894
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.
Upvotes: 13
Reputation: 6703
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)
Upvotes: 31