Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Long story short - <strong>use another constructor for your InputStreamBody that lets you specify the mime type you wish to use</strong>. If you don't, the parts in your multipart request will not have a <code>Content-Type</code> specified (see below for details). Consequently, the server does not know what type the file is, and in your case might be refusing to accept it (mine accepted them anyway, but I assume this is driven by config). If this still doesn't work, you might have a server-side issue.</p> <p>Note: Changing the <code>Content-Type</code> of the request itself to anything but <code>multipart/form-data; boundary=someBoundary</code> renders the request invalid; the server will not correctly parse the multipart parts.</p> <p>Long story long - here's my findings.</p> <p>Given the following code:</p> <pre><code>byte[] data = "&lt;someXml /&gt;".getBytes(); multipartContent.addPart("uploadedFile", new InputStreamBody(new ByteArrayInputStream(data), "text/xml", "somefile.xml")); multipartContent.addPart("otherPart", new StringBody("bar", "text/plain", Charset.forName("UTF-8"))); multipartContent.addPart("foo", new FileBody(new File("c:\\foo.txt"), "text/plain")); </code></pre> <p>The HttpClient posts the following payload (captured w/ Wireshark):</p> <pre><code>POST /upload.php HTTP/1.1 Transfer-Encoding: chunked Content-Type: multipart/form-data; boundary=SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3 Host: thehost.com Connection: Keep-Alive User-Agent: Apache-HttpClient/4.1-alpha2 (java 1.5) --SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3 Content-Disposition: form-data; name="uploadedFile"; filename="someXml.xml" Content-Type: text/xml Content-Transfer-Encoding: binary &lt;someXml /&gt; --SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3 Content-Disposition: form-data; name="otherPart" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit yo --SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3 Content-Disposition: form-data; name="foo"; filename="foo.txt" Content-Type: text/plain Content-Transfer-Encoding: binary Contents of foo.txt --SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3-- </code></pre> <p>On the server, the following PHP script:</p> <pre><code>&lt;?php print_r($_FILES); print_r($_REQUEST); </code></pre> <p>spitted out the following:</p> <pre><code>Array ( [uploadedFile] =&gt; Array ( [name] =&gt; someXml.xml [type] =&gt; text/xml [tmp_name] =&gt; /tmp/php_uploads/phphONLo3 [error] =&gt; 0 [size] =&gt; 11 ) [foo] =&gt; Array ( [name] =&gt; foo.txt [type] =&gt; text/plain [tmp_name] =&gt; /tmp/php_uploads/php58DEpA [error] =&gt; 0 [size] =&gt; 21 ) ) Array ( [otherPart] =&gt; yo ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload