Jersey Client post binary data application-octet/stream
Asked Answered
F

1

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.

Fagot answered 7/8, 2013 at 21:11 Comment(0)
P
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));
Pyrogenic answered 8/8, 2013 at 1:51 Comment(1)
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));Fagot

© 2022 - 2024 — McMap. All rights reserved.