9

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.

fernandospr
  • 2,541
  • 1
  • 18
  • 37

1 Answers1

9

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));
Septem
  • 3,484
  • 1
  • 18
  • 19
  • 2
    Thanks, I end up using: Builder webResourceBuilder = webResource.getRequestBuilder(); webResourceBuilder.type(MediaType.APPLICATION_OCTET_STREAM); byte[] buffer = {'a','b','c'}; ClientResponse response = webResourceBuilder.post(ClientResponse.class, new ByteArrayInputStream(buffer)); – fernandospr Aug 08 '13 at 14:06