Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How call PUT through Jersy REST client with null entity

I want to call some "upgrade" REST API through Jersy client, which is declared as PUT and does not require any body content.

But when I request this API as below:

webTarget.request().put(Entity.json(null));

It gives error as below:

Entity must not be null for http method PUT.

So need to know, is there any way in jersy client to call PUT method with null Entity.

like image 1000
Atul Kumar Avatar asked Jan 13 '16 06:01

Atul Kumar


3 Answers

I ran into the same error, and solved it by using an empty text entity:

Entity<?> empty = Entity.text("");
webTarget.request().put(empty);
like image 116
Peter Walser Avatar answered Oct 30 '22 10:10

Peter Walser


You can use webTarget.request().put(Entity.json(""));
It works for me.

like image 44
Shashi Avatar answered Oct 30 '22 09:10

Shashi


You can configure the client property

SUPPRESS_HTTP_COMPLIANCE_VALIDATION

By default, Jersey client runtime performs certain HTTP compliance checks (such as which HTTP methods can facilitate non-empty request entities etc.) in order to fail fast with an exception when user tries to establish a communication non-compliant with HTTP specification. Users who need to override these compliance checks and avoid the exceptions being thrown by Jersey client runtime for some reason, can set this property to true. As a result, the compliance issues will be merely reported in a log and no exceptions will be thrown.

[...]

Client client = ClientBuilder.newClient();
client.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
WebTarget target = client.target("...");
Response response = target.request().put(Entity.json(null));
like image 20
Paul Samsotha Avatar answered Oct 30 '22 11:10

Paul Samsotha