Jersey Client post binary data application-octet/stream

fernandospr picture fernandospr · Aug 7, 2013 · Viewed 10.6k times · Source

I would like to perform a post with binary data using Jersey Client.

The equivalent with curl would be:

curl -v --header "Content-Type:application/octet-stream" --data-binary "abc" http://example.com

I could not find how to do it in the official docs: http://jersey.java.net/documentation/latest/user-guide.html#client

Thanks.

Answer

Septem picture Septem · Aug 8, 2013

I think you can invoke a POST request with Entity which encapsulates binary data like this:

Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("http://example.com/rest");
Response response = webTarget.request(MediaType.TEXT_PLAIN_TYPE)
                .post(Entity.entity("abc", MediaType.APPLICATION_OCTET_STREAM));