Reputation: 77
I do not seem to be getting the entire json response from httpclient. I am hitting an api I'm running locally like so:
curl -i -X POST http://localhost:8098/<api location> -F "files=@<filename>"
And my response looks like this:
{"data":[<bunch of json>]
But when I try to post the exact same file with httpclients, I get this response:
{"data":[]}
What am I doing wrong? Here is my java code. Thank you!
public CloseableHttpResponse submit (File file) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(API_LOCATION + API_BASE);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("ISO xml file", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
HttpEntity multipartEntity = builder.build();
post.setEntity(multipartEntity);
CloseableHttpResponse response = client.execute(post);
System.out.println("response: " + IOUtils.toString(response.getEntity().getContent(),"UTF-8"));
client.close();
return response;
}
Upvotes: 1
Views: 455
Reputation: 77
As Andreas and Danilo mentioned in the comments:
In curl you name the field files, but in Java you name it ISO xml file. Since server only looks for files, it see nothing, and responds with nothing. - Andreas
What about the parameter's name? On curl you used 'files' and on http client you used 'ISO xml file'. Try changing it to 'file'. - Danilo
I needed to change "ISO xml file" to "files" and it worked.
Upvotes: 1