deyo.vuk
deyo.vuk

Reputation: 180

Send file via Jersey and x-www-form-urlencoded

I'm trying to invoke REST service with the following client code with the idea to send some String message details as well as the attachment file:

ClientConfig config = new DefaultClientConfig();
config.getClasses().add(FormProvider.class);
Client client = Client.create(config);
WebResource webResource = client.resource("http://some.url/path1/path2");

File attachment = new File("./file.zip");

FormDataBodyPart fdp = new FormDataBodyPart(
            "content", 
            new ByteArrayInputStream(Base64.encode(FileUtils.readFileToByteArray(attachedLogs))),
            MediaType.APPLICATION_OCTET_STREAM_TYPE);
form.bodyPart(fdp);

ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);     

The server I'm targeting accepts Base64 encoded content, so that is why the additional transferring from File to ByteArray.

Also, I have found that class com.sun.jersey.core.impl.provider.entity.FormProvider is noted with both for production and consuming of "x-www-form-urlencoded" requests.

@Produces({"application/x-www-form-urlencoded", "*/*"})
@Consumes({"application/x-www-form-urlencoded", "*/*"})

But still I end-up with the following stacktrace:

com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, application/x-www-form-urlencoded, was not found at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149) ~[jersey-client-1.9.1.jar:1.9.1]
at com.sun.jersey.api.client.Client.handle(Client.java:648) ~[jersey-client-1.9.1.jar:1.9.1]
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670) ~[jersey-client-1.9.1.jar:1.9.1]
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.9.1.jar:1.9.1]
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563) ~[jersey-client-1.9.1.jar:1.9.1]

Any help on this one?

Upvotes: 3

Views: 13167

Answers (2)

deyo.vuk
deyo.vuk

Reputation: 180

I managed to get the thing working on the client side. The problem was that I was forcing sending of file as separate message body part, while x-www-form-urlencoded is actually packing all of the data as parameters in the query that is the entire body.

So the working client code if you want to send attachment via Jersey post method would be:

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.resource("http://some.url/path1/path2");

MultivaluedMapImpl values = new MultivaluedMapImpl();
values.add("filename", "report.zip");
values.add("text", "Test message");
values.add("content", new String(Base64.encode(FileUtils.readFileToByteArray(attachedLogs))));
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, values);

The Apache Commons' Base64 encoder was required in my case to transform file into encoded byte array, not sure if this is general requirement.

Upvotes: 4

Alex Stybaev
Alex Stybaev

Reputation: 4693

try using Multipart/form-data instead of application/x-www-form-urlencoded. This tutorial might help.

Upvotes: 1

Related Questions