0

I am trying to send a two binary file to one of the REST API. But I get 400 bad request response from the end point.

Need to send below key and values to endpoint.

userForm - user.xml
structureForm - structure.xdp

Below is the java code, [UPDATED CODE]

HttpPost request = new HttpPost(url);

File userForm  = new File("D:\\Downloads\\user.xml");
LOG.info("length ---->" + userForm.length()); // See valid file size
HttpEntity userFormEntity = MultipartEntityBuilder.create()
    .addPart("userForm", new FileBody(userForm))
    .build();

File structureFile = new File("D:\\Downloads\\structure.xdp");
LOG.info("length structureFile ---->" + structureFile.length()); // See valid file size
HttpEntity structureEntity = MultipartEntityBuilder.create()
    .addPart("structureForm", new FileBody(structureFile))
    .build();



if (userFormEntity != null && structureEntity != null) {
    request.setEntity(userFormEntity);
    request.setEntity(structureEntity);

}

final CloseableHttpClient httpClient = HttpClientBuilder.create().build();

CloseableHttpResponse response = httpClient.execute(request);

Seemed like the key 'userForm' and 'structureForm' are not going properly to end point. Is it correct way to send the key?

It is working when I try to submit through postman as below enter image description here

ezhil
  • 977
  • 6
  • 15
  • 36
  • Semi-related: you're using an extremely old API. Consider updating to a modern, better supported HTTP client library. – Sotirios Delimanolis Dec 31 '21 at 14:52
  • can you suggest me recent APIs pls – ezhil Dec 31 '21 at 14:57
  • The recent versions of https://hc.apache.org/index.html. Or Java 11’s HTTP client library. – Sotirios Delimanolis Dec 31 '21 at 15:00
  • Did you mean - https://hc.apache.org/httpcomponents-core-5.1.x/current/httpcore5/apidocs/. Can you help me to get some sample or example please? – ezhil Dec 31 '21 at 15:04
  • @ezhil please check this question https://stackoverflow.com/questions/2469451/upload-files-from-java-client-to-a-http-server one of the answers has example of sending several files in one request – Ivan Dec 31 '21 at 15:20
  • Does this answer your question? [Upload files from Java client to a HTTP server](https://stackoverflow.com/questions/2469451/upload-files-from-java-client-to-a-http-server) – GreyBeardedGeek Dec 31 '21 at 15:34
  • Thank you @GreyBeardedGeek and Sotirios. But still I see 400 response from API. Let me update the code – ezhil Dec 31 '21 at 17:09

0 Answers0