# Java Rest Client

**URL:** <https://discuss.elastic.co/t/java-rest-client/67695>\
**Category:** Elasticsearch\
**Created:** [December 1, 2016, 1:35am UTC](https://discuss.elastic.co/t/java-rest-client/67695 "2016-12-01T01:35:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![szaharici](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@szaharici](https://discuss.elastic.co/u/szaharici)\
**Post date:** [December 1, 2016, 1:35am UTC](https://discuss.elastic.co/t/java-rest-client/67695/1 "2016-12-01T01:35:14Z")

</div>

Hi guys,

I have been trying unsuccessfully to do a bulk insert with the new Java rest client... Does anyone have a working example of a bulk insert using the rest client ?  
I can do searches and index single documents without any issue but no bulk operations. I have tried with pretty much all the types of http entities but I am always getting a 400.  
Here is a small example:  
StringBuilder batch = new StringBuilder();  
String bulkline = "{"index": {}}\n";  
batch.append(bulkline);  
batch.append("{"kukta": "haha"}");  
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();  
HttpEntity entity = new NStringEntity(batch.toString(), ContentType.APPLICATION\_JSON);  
Response indexResponse = restClient.performRequest(  
"POST",  
"/kukta/test/\_bulk",  
Collections.\<String, String\>emptyMap(),  
entity);

Thanks a lot,  
Simeon

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 1, 2016, 6:14am UTC](https://discuss.elastic.co/t/java-rest-client/67695/2 "2016-12-01T06:14:05Z")

</div>

May be this code can help you. Look at bulk method.

[https://github.com/dadoonet/fscrawler/blob/master/src/main/java/fr/pilato/elasticsearch/crawler/fs/client/ElasticsearchClient.java](https://github.com/dadoonet/fscrawler/blob/master/src/main/java/fr/pilato/elasticsearch/crawler/fs/client/ElasticsearchClient.java)

---

<div class="post-metadata">

**Author:** ![szaharici](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@szaharici](https://discuss.elastic.co/u/szaharici)\
**Post date:** [December 1, 2016, 1:07pm UTC](https://discuss.elastic.co/t/java-rest-client/67695/3 "2016-12-01T13:07:28Z")

</div>

Thanks a lot!  
I was missing a trailing newline in my bulk request

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 29, 2016, 1:07pm UTC](https://discuss.elastic.co/t/java-rest-client/67695/4 "2016-12-29T13:07:29Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
