Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse_exception - request body is required

I'm trying to insert a JSON data file in my elasticsearch instance.

curl -s -H “Content-Type: application/x-ndjson” -XPOST localhost:9200/_bulk —-data-binary “@restaurants.json”; echo

However, after executing this command I get an error that says

{"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}

The JSON file basically has an array of the below object. Only thing is that I've put just one object here to save space. However, there are more than one objects present.

Structure is like given below;

[
   {
      "address": {
         "building": "351",
         "coord": [
            -73.98513559999999,
            40.7676919
         ],
         "street": "West   57 Street",
         "zipcode": "10019"
      },
      "borough": "Manhattan",
      "cuisine": "Irish",
      "name": "Dj Reynolds Pub And Restaurant",
      "grades": [
         {
            "date": {
               "$date": "2014-09-06T00:00:00.000Z"
            },
            "grade": "A",
            "score": 2
         },
         {
            "date": {
               "$date": "2013-07-22T00:00:00.000Z"
            },
            "grade": "A",
            "score": 11
         },
         {
            "date": {
               "$date": "2012-07-31T00:00:00.000Z"
            },
            "grade": "A",
            "score": 12
         },
         {
            "date": {
               "$date": "2011-12-29T00:00:00.000Z"
            },
            "grade": "A",
            "score": 12
         }
      ],
      "id": "30191841"
   }
]
like image 716
Vivek Malhotra Avatar asked Jan 06 '18 05:01

Vivek Malhotra


2 Answers

The bulk API requires one document per line, which means you can't have newlines in your documents. Try striping all the white spaces from the JSON you're submitting. You are also just submit a stream of documents, and not a JSON array of objects. See Bulk API documentation here: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

like image 113
Oleksi Avatar answered Jan 12 '23 06:01

Oleksi


I had this same issue in Windows and am posting this here for anyone that comes across the same question with this error referenced.

{"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}

I found at least two causes:

  1. cURL doesn't know where the file you are trying to load is:

In this case, make sure to run cURL from the directory where the file is located. Try executing the following command and making sure you see the file.

more file.json

  1. Windows command line DOES NOT support single quotes within the cURL command line:

BAD

curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/_bulk' --data-binary '@file.json'

GOOD

curl -H "Content-Type: application/x-ndjson" -XPOST "localhost:9200/_bulk" --data-binary "@file.json"

like image 30
Ken Tice Avatar answered Jan 12 '23 07:01

Ken Tice