Java http post for multipart data
<p>I am using Apache HttpComponents v4.3.6 (maven httpclient and httpmime). I need to upload a file data as multipart. The Fiddler command, which works, looks like the following.</p>

<p><strong>Request header on Fiddler:</strong></p>

<pre><code>Content-Type: multipart/form-data; boundary=c2d7073062e24d86ad739647574e14b9
Accept: application/json
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0) Gecko/20100101 Firefox/22.0
</code></pre>

<p><strong>Request body on Fiddler:</strong></p>

<pre><code>--c2d7073062e24d86ad739647574e14b9
Content-Disposition: form-data; name="categoryFile"; filename="self-classification-categories.tsv"

585743  583099  Existing Catrali 4Category ch   Description of 4 Existing Category  false   false   false   Some notes 4 relating to Existing Category
--c2d7073062e24d86ad739647574e14b9--
</code></pre>

<p>Where the actual content of the file is :</p>

<pre><code>585743  583099  Existing Catrali 4Category ch   Description of 4 Existing Category  false   false   false   Some notes 4 relating to Existing Category
</code></pre>

<p>Now, I'm trying to implement this post request(described above) with Apache Http Client, but have no idea how to actually do it. To convert the above request into Java (1.8), I tried:  <strong>( Boundary value : c2d7073062e24d86ad739647574e14b9)</strong></p>

<pre><code>httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost( createPostURI( host, path ) );

httpPost.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0) Gecko/20100101 Firefox/22.0");
httpPost.setHeader("Content-Type", "multipart/form-data; boundary=c2d7073062e24d86ad739647574e14b9");
httpPost.setHeader("Accept", "application/json");


String fileContent = "--c2d7073062e24d86ad739647574e14b9\r\nContent-Disposition: form-data; name=\"categoryFile\"; filename=\"self-classification-categories.tsv\""+
                "\r\n\r\n"+
                "585743 583099  Existing Catrali 4Category ch   Description of 4 Existing Category  false   false   false   Some notes 4 relating to Existing Category"
                +"\r\n--c2d7073062e24d86ad739647574e14b9--";

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setBoundary("c2d7073062e24d86ad739647574e14b9");
builder.addTextBody("body", fileContent,ContentType.MULTIPART_FORM_DATA);

HttpEntity entity =  builder.build();
httpPost.setEntity( entity );

response = httpclient.execute( httpPost, new GenericResponseHandler() );
</code></pre>

<p><strong>I am sure that the way I am crafting the request-body in Java is wrong.</strong> Hence, I seeing 403 error which is kind of misleading as the REST api that I am calling throws this error when it does not see the expected format.I would appreciate any help.</p>

<p>Thanks in advance.</p>