Java post json file to elasticsearch with apache http

apache, elasticsearch, http-post, json

Solution

MultipartEntityBuilder adds mime header fields to the request body. This is not valid for the bulk api. Also multipart request body has other meta information such as boundary in the request body it would result in a bad request

You could use a FileEntity to achieve the same the code Snippet would be something on these line :

private void sendFile() throws Exception{
    String fileName = "C://others2.json";
    File jsonFile = new File(fileName);

    HttpEntity  entity = new FileEntity(jsonFile);

    HttpPost post = new HttpPost("http://localhost:9200/_bulk");
    post.setEntity(entity);

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    HttpClient client = clientBuilder.build();

    post.addHeader("content-type", "text/plain");
    post.addHeader("Accept","text/plain");

    HttpResponse response = client.execute(post);

    System.out.println("Response: " + response);
}

Problem

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

Original source