I am able to run curl to upload a file to elasticsearch with curl -XPUT localhost:9200/_bulk --data-binary @other2.json
The content of the file is
{"index":{"_index":"movies","_type":"movie","_id":1}}\n
{"title": "Movie1","director": "director1","year":1962}\n
{"index":{"_index":"movies","_type":"movie","_id":2}}\n
{"title": "Movie2","director": "director2","year": 1972}\n
{"index":{"_index":"movies","_type":"movie","_id":3}}\n
{"title": "Movie3","director": "director3","year": 1972}\n
with a new line at the end.
However I am unable to post the same file with apache http client
public static void main(String[] args) throws Exception {
JsonResponse http = new JsonResponse();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendFile();
}
private void sendFile() throws Exception{
String fileName = "C:\\other2.json";
File jsonFile = new File(fileName);
HttpEntity entity = MultipartEntityBuilder.create()
.addBinaryBody("file", jsonFile,org.apache.http.entity.ContentType.APPLICATION_JSON, jsonFile.getName())
.build();
HttpPost post = new HttpPost("http://localhost:9200/_bulk");
post.setEntity(entity);
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
HttpClient client = clientBuilder.build();
post.addHeader("content-type", "application/json");
post.addHeader("Accept","application/json");
HttpResponse response = client.execute(post);
System.out.println("Response: " + response);
}
The final output is uninformative
Testing 2 - Send Http POST request
Response: HTTP/1.1 400 Bad Request [Content-Type: application/json;
charset=UTF-8, Content-Length: 152] org.apache.http.impl.execchain.ResponseEntityWrapper@124bec88
Any help would be appreciated