I'm working with an external REST API on a test system. There is an upload path I'm trying to implement and I am using the Apache HTTPClient. I tried it with the following postman configuration and it works perfectly:
The upload works fine like that.
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut request = new HttpPut("../upload") // placeholder url, not the real one
request.setHeader("Content-type", "multipart/form-data");
request.setHeader(HttpHeaders.AUTHORIZATION, "HEY I AM A TOKEN");
FileBody fileBody = new FileBody(new File("dummy.pdf"), ContentType.create("application/pdf"));
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("file", fileBody);
request.setEntity(builder.build());
CloseableHttpResponse response = httpClient.execute(request);
I always get a specific error from the rest api:
File could not have been parsed
The documentation of the upload path mentions the following details:
I think I do every point of this list in my request - so what is the difference between the postman request and my own java http request?
What do I miss?
EDIT:
The same code with OkHTTP works. Still don't know why it does not work with Apache HttpClient.
OkHttpClient testClient = new OkHttpClient();
File testfile = new File("dummy.pdf");
RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM).addFormDataPart("file","dummy.pdf",
RequestBody.create(MediaType.parse("application/pdf"),testfile)).build();
Request testreq = new Request.Builder()
.url("../upload")
.header("Authorization", "HI I AM A TOKEN")
.put(body)
.build();
Response testres = testClient.newCall(testreq).execute();
I had to remove this line
request.setHeader("Content-type", "multipart/form-data");
to make it work. I dont really know why so if someone can help me out with this and give some explanation I'm up for it. For the moment I'm happy because it works now.