I'm trying to call this specific method http://developers.box.com/docs/#files-upload-a-file in Box API with the help of Google HTTP Client library v1.14.1. Currently I see no way of doing this.
If I was using http://hc.apache.org/httpclient-3.x/methods/multipartpost.html, I would add 2 items of StringPart and 1 item of FilePart.
In Google HTTP Client library I see only MultipartContent and Part classes that do not seem to be able to handle pure name/value pairs, as StringPart referenced above.
Here is an excerpt from Apache HTTP Client examples:
HttpPost httppost = new HttpPost("http://localhost:8080" +
"/servlets-examples/servlet/RequestInfoExample");
FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);
I want to accomplish similar thing, but using Google HTTP Client. Any suggestions would be welcome!
After some investigation I found that I needed Content-Type: multipart/form-data for the Box API and appropriately build the request. It was not possible with the version of Google HTTP Client I was using, so I implemented MultipartFormDataContent class myself and it fits perfectly to the library. Here is the full listing of the class. Maybe it can be included into the library.
I'd been struggling with this small limitation in the google-http-java-client for months and I came up time ago with an ugly and redundant version of the old MultipartRelatedContent class. Now I updated the Google library to the latest 1.15.0-rc and I've found out that it was quite easy to subclass the MultipartContent and make something decent that should work with any kind of "multipart/form-data" content.
At the moment you can find it here: https://github.com/marcosalis/kraken/blob/master/kraken_lib/src/main/java/com/google/api/client/http/MultipartFormDataContent.java
A short usage example:
"image" will be the value of the "name" key in the "content-disposition" header, and the file name (image.jpg in this case) will be the (optional) value for the "filename" key.
In the same way you can add as many different parts as you want (also nesting other "multipart/mixed" contents, just making sure you use a different boundary string) to the first level multipart content.