Categories

HTTP 400: Bad Request

An HTTP 400 implies the problem is not with Elasticsearch, but rather with the request to Elasticsearch.
Last updated
July 7, 2023

An HTTP 400 Bad Request can be caused by a variety of problems. However, it is generally a client-side issue. An HTTP 400 implies the problem is not with Elasticsearch, but rather with the request to Elasticsearch.

For example, if you have a mapping that expects a number in a particular field, and then index a document with some other data type in that field, Elasticsearch will reject it with an HTTP 400:

<div class="code-snippet-container">
<a fs-copyclip-element="click" href="#" class="btn w-button code-copy-button" title="Copy">
<img class="copy-image" src="https://global-uploads.webflow.com/63c81e4decde60c281417feb/6483934eeefb356710a1d2e9_icon-copy.svg" loading="lazy" alt="">
<img class="copied-image" src="https://assets-global.website-files.com/63c81e4decde60c281417feb/64839e207c2860eb9e6aa572_icon-copied.svg" loading="lazy" alt="">
</a>
<div class="code-snippet">
<pre><code fs-codehighlight-element="code" fs-copyclip-element="copy-this-2" class="hljs language-javascript"># Create a document with a field called "views" and the number 0POST /myindex/mytype/1?pretty -d '{"views":0}'
{
 "_index" : "myindex",
 "_type" : "mytype",
 "_id" : "1",
 "_version" : 1,
 "_shards" : {
   "total" : 2,
   "successful" : 2,
   "failed" : 0
 },
 "created" : true
}

# Elasticsearch has automagically determined the "views" field to be a long (integer) data type:GET /myindex/_mapping?pretty
{
 "myindex" : {
   "mappings" : {
     "mytype" : {
       "properties" : {
         "views" : {
           "type" : "long"
         }
       }
     }
   }
 }
}

# Try to create a new document with a string value instead of a long in the "views" field:POST /myindex/mytype/2?pretty -d '{"views":"zero"}'
{
 "error" : {
   "root_cause" : [ {
     "type" : "mapper_parsing_exception",
     "reason" : "failed to parse [views]"
   } ],
   "type" : "mapper_parsing_exception",
   "reason" : "failed to parse [views]",
   "caused_by" : {
     "type" : "number_format_exception",
     "reason" : "For input string: \"zero\""
   }
 },
 "status" : 400
}</code></pre>
</div>
</div>

The way to troubleshoot an HTTP 400 error is to read the response carefully and understand which part of the request is raising the exception. That will help you to identify a root cause and remediate.

View code snippet
Close code snippet