nguyenducnhaty / google-http-java-client

Automatically exported from code.google.com/p/google-http-java-client
0 stars 0 forks source link

Support content of type multipart/form-data #107

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?
multipart/form-data: http://tools.ietf.org/html/rfc2388

Java environments (e.g. Java 6, Android 2.3, App Engine, or All)?
All

Please describe the feature requested.
Add MultipartFormDataContent to the set of HttpContent classes.

Original issue reported on code.google.com by titogar...@gmail.com on 9 May 2012 at 11:41

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 2 Oct 2012 at 1:43

GoogleCodeExporter commented 9 years ago
Is there a current work around for this in this lib?

Original comment by chris.mark.jenkins@gmail.com on 16 Apr 2013 at 7:23

GoogleCodeExporter commented 9 years ago
I needed the same feature for my application, and I ended up modifying the 
MultipartRelatedContent class, following the RFC 2388 specifications 
(http://tools.ietf.org/html/rfc2388). It is not thoroughly tested and probably 
not the best implementation possible, but I've been using it since months and 
it's working correctly with 'multipart/form-data' requests.

You can find it in my Android utils library, released under Apache 2.0 license 
(so you can freely use and modify it), here:
https://github.com/Luluvise/droid-utils/blob/master/src/com/github/luluvise/droi
d_utils/network/MultipartFormDataContent.java

Original comment by fas...@gmail.com on 17 Apr 2013 at 12:34

GoogleCodeExporter commented 9 years ago
Thanks for sharing.  Would you be interested in contributing to the library 
directly?

For details about the process for contributing, please take a look here:

https://code.google.com/p/google-http-java-client/wiki/BecomingAContributor#Cont
ributor_License_Agreements

Original comment by yan...@google.com on 17 Apr 2013 at 2:01

GoogleCodeExporter commented 9 years ago
Yeah, of course. Will do!

Original comment by fas...@gmail.com on 18 Apr 2013 at 4:15

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I have made several improvements to my class and made it usable with the latest 
version (1.15.0-rc) of the library by directly subclassing MultipartContent.
I will soon submit the patch for code review, at the moment the class can be 
found here:
https://github.com/Luluvise/droid-utils/blob/master/src/com/google/api/client/ht
tp/MultipartFormDataContent.java

Note that the MultipartContent class itself can sometimes be successfully used 
to construct "multipart/form-data" content.

That can be accomplished in the following way:
MultipartContent content = new MultipartContent();
content.setMediaType(new HttpMediaType("multipart/form-data"));
Part part = new Part(*yourHttpContent*);
HttpHeaders headers = new HttpHeaders(); // or retrieve existing headers
headers.setAcceptEncoding(null);
headers.set("content-disposition", "form-data; name=\"field\"");
part.setHeaders(headers);
content.addPart(part);

However, by doing so some specifications in the RFC2388 have to be implemented 
manually, such as the mandatory header "content-disposition" for each part, and 
some redundant headers are appended for every part even though they are not 
needed (i.e. content-length).

Original comment by fas...@gmail.com on 17 Jun 2013 at 3:51

GoogleCodeExporter commented 9 years ago
Hi,

The code proposed seems OK for a workaround. I also did my own workaround 
delegating on  org.apache.http.entity.mime.MultipartEntity. (See attached).

However, in order to build it into the real google-http-java-client, I am 
missing:
- Code reuse as class inheritance, not as copy-paste. I would expect something 
like a MultipartContent base class, with specific MultipartRelatedContent and 
MultipartFormDataContent child classes.
- I think for MultipartFormDataContent it is interesting to accept data 
objects, as JsonHttpContent does.

Original comment by titogar...@gmail.com on 19 Jun 2013 at 11:06

Attachments:

GoogleCodeExporter commented 9 years ago
As a workaround I used Marco's MultipartFormDataContent class, worked well for 
me for creating attachments in Salesforce :) For this:
http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sobject_insert
_update_blob.htm

Hopefully a class for posting form-data would be part of the lib some day.

Thanks

Original comment by ale...@yoxel.com on 3 Jul 2013 at 1:26