java- apache http client-query regarding submission of files as part of a multi part post request

StackOverflow https://stackoverflow.com/questions/9046285

  •  03-12-2019
  •  | 
  •  

Question

I am trying to use the latest Apache HTTP Client (v4.x) to send a multi part POST request- the example code provided with the docs gives the following code sample (somewhat modified) to make a POST request--

        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);

What I am confused about is, if I have multiple files to be added, then in the code

   reqEntity.addPart("bin", bin);

what does the first string represent? Is it the name of the file which is being sent as part of multi part post?

Was it helpful?

Solution

Multipart Form requests can have several parts, and each part is given a name (similar to a regular form request). This name can be used on the server side to retrieve a specific part, given the name. Good details are available in RFC 2388:

    3. Definition of multipart/form-data

       The media-type multipart/form-data follows the rules of all multipart
       MIME data streams as outlined in [RFC 2046].  In forms, there are a
       series of fields to be supplied by the user who fills out the form.
       Each field has a name. Within a given form, the names are unique.

       "multipart/form-data" contains a series of parts. Each part is
       expected to contain a content-disposition header [RFC 2183] where the
       disposition type is "form-data", and where the disposition contains
       an (additional) parameter of "name", where the value of that
       parameter is the original field name in the form. For example, a part
       might contain a header:

            Content-Disposition: form-data; name="user"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top