# How to build bulk request body

**URL:** <https://discuss.elastic.co/t/how-to-build-bulk-request-body/12534>\
**Category:** Elasticsearch\
**Created:** [June 25, 2013, 3:36am UTC](https://discuss.elastic.co/t/how-to-build-bulk-request-body/12534 "2013-06-25T03:36:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mega](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mega/32/1631_2.png) [@mega](https://discuss.elastic.co/u/mega)\
**Post date:** [June 25, 2013, 3:36am UTC](https://discuss.elastic.co/t/how-to-build-bulk-request-body/12534/1 "2013-06-25T03:36:34Z")

</div>

In my project i need to making bulk requests to elasticsearch, that means  
making http post requests in program instead of using curl.

I already set content-type to json, and the body is like: '{ "index" :  
{"\_index":"test","\_type":"one","\_id":"1"}}\n{"name":"test","id":"1" }\n',  
and got a error: {"error":"ActionRequestValidationException[Validation  
Failed: 1: no requests added;]","status":500}

In terminal using curl -XPOST localhost:9200/\_bulk -d '{ "index" :  
{"\_index":"test","\_type":"one","\_id":"1"}}\n{"name":"test","id":"1" }\n'  
got the same error, and that's ok:  
curl -XPOST localhost:9200/\_bulk -d '{ "index" :  
{"\_index":"test","\_type":"one","\_id":"1"}}  
{"name":"test","id":"1" }  
'  
Right now i have to build request body by my own, the guide  
([http://www.elasticsearch.org/guide/reference/api/bulk/](http://www.elasticsearch.org/guide/reference/api/bulk/)) said use newline  
character \n, that'sexactly what i did, and it seems wrong, i wanna know  
how to describe newline character in request body.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [June 25, 2013, 6:00am UTC](https://discuss.elastic.co/t/how-to-build-bulk-request-body/12534/2 "2013-06-25T06:00:05Z")

</div>

You need to send in Bulk a real Line return character (which we represent with \n).  
You can create a file with proper line endings and then use it from curl.

$ cat requests  
{ "index" : { "\_index" : "test", "\_type" : "type1", "\_id" : "1" } }  
{ "field1" : "value1" }  
$ curl -s -XPOST localhost:9200/\_bulk --data-binary @requests; echo  
{"took":7,"items":[{"create":{"\_index":"test","\_type":"type1","\_id":"1","\_version":1,"ok":true}}]}

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 25 juin 2013 à 05:36, mega [yuhg2310@gmail.com](mailto:yuhg2310@gmail.com) a écrit :

In my project i need to making bulk requests to elasticsearch, that means making http post requests in program instead of using curl.

I already set content-type to json, and the body is like: '{ "index" : {"\_index":"test","\_type":"one","\_id":"1"}}\n{"name":"test","id":"1" }\n', and got a error: {"error":"ActionRequestValidationException[Validation Failed: 1: no requests added;]","status":500}

## In terminal using curl -XPOST localhost:9200/\_bulk -d '{ "index" : {"\_index":"test","\_type":"one","\_id":"1"}}\n{"name":"test","id":"1" }\n' got the same error, and that's ok: curl -XPOST localhost:9200/\_bulk -d '{ "index" : {"\_index":"test","\_type":"one","\_id":"1"}} {"name":"test","id":"1" } ' Right now i have to build request body by my own, the guide ([http://www.elasticsearch.org/guide/reference/api/bulk/](http://www.elasticsearch.org/guide/reference/api/bulk/)) said use newline character \n, that'sexactly what i did, and it seems wrong, i wanna know how to describe newline character in request body.

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mega](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mega/32/1631_2.png) [@mega](https://discuss.elastic.co/u/mega)\
**Post date:** [June 25, 2013, 7:02am UTC](https://discuss.elastic.co/t/how-to-build-bulk-request-body/12534/3 "2013-06-25T07:02:14Z")

</div>

I forgot the final newline character in my code, that's why it's fail, and  
obviously curl treat "\n" as character "" and "n", i figured out by  
changing the es source code and print out the request byte and compare  
the output.

I am misguided by curl and ignore my own mistake, waist a lot of time,  
shame on me.

On Tuesday, June 25, 2013 2:00:05 PM UTC+8, David Pilato wrote:

> You need to send in Bulk a real Line return character (which we represent  
> with \n).  
> You can create a file with proper line endings and then use it from curl.
> 
> $ cat requests  
> { "index" : { "\_index" : "test", "\_type" : "type1", "\_id" : "1" } }  
> { "field1" : "value1" }  
> $ curl -s -XPOST localhost:9200/\_bulk --data-binary @requests; echo  
> {"took":7,"items":[{"create":{"\_index":"test","\_type":"type1","\_id":"1","\_version":1,"ok":true}}]}
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 25 juin 2013 à 05:36, mega \<[yuhg...@gmail.com](mailto:yuhg...@gmail.com) \<javascript:\>\> a écrit :
> 
> In my project i need to making bulk requests to elasticsearch, that means  
> making http post requests in program instead of using curl.
> 
> I already set content-type to json, and the body is like: '{ "index" :  
> {"\_index":"test","\_type":"one","\_id":"1"}}\n{"name":"test","id":"1" }\n',  
> and got a error: {"error":"ActionRequestValidationException[Validation  
> Failed: 1: no requests added;]","status":500}
> 
> In terminal using curl -XPOST localhost:9200/\_bulk -d '{ "index" :  
> {"\_index":"test","\_type":"one","\_id":"1"}}\n{"name":"test","id":"1" }\n'  
> got the same error, and that's ok:  
> curl -XPOST localhost:9200/\_bulk -d '{ "index" :  
> {"\_index":"test","\_type":"one","\_id":"1"}}  
> {"name":"test","id":"1" }  
> '  
> Right now i have to build request body by my own, the guide (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/bulk/)) said use newline  
> character \n, that'sexactly what i did, and it seems wrong, i wanna know  
> how to describe newline character in request body.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 2:29am UTC](https://discuss.elastic.co/t/how-to-build-bulk-request-body/12534/4 "2017-07-06T02:29:37Z")

</div>


