4

I am trying to perform a HTTP Post Request in Java using the Apache API.

With curl the request looks like this

curl https://host/upload
-X POST
-H "Authorization: Bearer xxx"
-H "Content-Type: multipart/form-data"
-H "Accept: application/json"
-F "file=@{PathToImage}" -F "type=file" 

While this work fine when running it with CURL the server returns a 500er result when running it with the following Java code

    final HttpPost httppost = new HttpPost("https://host/upload");
    httppost.addHeader("Authorization", "Bearer xxx");
    httppost.addHeader("Accept", "application/json");
    httppost.addHeader("Content-Type", "multipart/form-data");

    final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    final File file = new File("c:\\tmp\\myfile.pdf");
    builder.addBinaryBody("file", file);
    builder.addTextBody("type", "file");
    final HttpEntity entity = builder.build();
    httppost.setEntity(entity);
    final HttpResponse response = httpclient.execute(httppost);
    httpclient.close();

Any idea what I am missing here?

Paul
  • 547
  • 4
  • 13
  • 30
  • try with addPartBody instead of addBinaryBody. – vermaji Nov 18 '19 at 05:40
  • try to remove ```httppost.addHeader("Content-Type", "multipart/form-data");``` – S.R. Nov 23 '19 at 06:16
  • 1
    @Paul, don't you have access to the called server logs? Did you check the response body for potential error details? You can also use a proxy like Fiddler and compare the requests generated from CURL vs. from Java... – Petr Bodnár Nov 24 '19 at 21:20

3 Answers3

2

Try syntax as baeldung multipart upload article suggest:

String textFileName = "c:\\tmp\\myfile.pdf";
final File file = new File(textFileName);
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, textFileName);
builder.addTextBody("type", "file", ContentType.DEFAULT_BINARY);

create a multipart entity is to use the addBinaryBody and AddTextBody methods. These methods work for uploading text, files, character arrays, and InputStream objects.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Thanks for the reply, but unfortunatelly I am still getting a 500er response. – Paul Nov 19 '19 at 06:47
  • @Paul it may be related to missing User-Agent, try adding `httppost.addHeader("User-Agent", "Any String will do");` – Ori Marko Nov 20 '19 at 05:50
2

Try something like this instead

        //import javax.net.ssl.HttpsURLConnection;
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        String encoding = Base64.getEncoder().encodeToString((user + ":" + psw).getBytes("UTF-8"));

        con.setRequestProperty("Authorization", String.format("Basic %s", encoding));
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", "Java client");

        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(val);
CoupFlu
  • 311
  • 4
  • 20
2

This question is similar. But I believe the answer is changing your addBinary to addPart.

final HttpPost httppost = new HttpPost("https://host/upload");
httppost.addHeader("Authorization", "Bearer xxx");
httppost.addHeader("Accept", "application/json");
httppost.addHeader("Content-Type", "multipart/form-data");

final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
final File file = new File("c:\\tmp\\myfile.pdf");
builder.addPart("file", new FileBody(file));
builder.addTextBody("type", "file");
final HttpEntity entity = builder.build();
httppost.setEntity(entity);
final HttpResponse response = httpclient.execute(httppost);
httpclient.close();
C. Smith
  • 172
  • 1
  • 4
  • 16