Elasticsearch 不支持 Content-Type header [application/x-www-form-urlencoded]

Content-Type header [application/x-www-form-urlencoded] is not supported on Elasticsearch

我以前有 ElasticSearch 5.2,刚刚升级到 6.0。

我正尝试按照指南 here 创建索引模板,但出现错误

Content-Type header [application/x-www-form-urlencoded] is not supported

我的查询是

curl -X PUT localhost:9200/_template/template_1 -d '
{
  "index_patterns": ["te*", "bar*"],
  "mappings": {
    "type1": {
      "properties": {
        "host_name": {
          "type": "keyword"
        }
      }
    }
  }
}'

要解决此问题,请添加 curl 选项 -H 'Content-Type: application/json'


此错误是由于 ElasticSearch 6.0 中引入的严格内容类型检查,如this post

中所述

Starting from Elasticsearch 6.0, all REST requests that include a body must also provide the correct content-type for that body.

解决方法是添加Content-Type: application/json header

curl -XPUT 'localhost:9200/_template/template_1' \
  -H 'Content-Type: application/json' \
  -d '**your query**'
"{ \"name\": { \"first\": {} }, \"address\": [ { \"address1\":\"lane\" } ] } "

在Windows中,当你给JSON作为参数时,只使用双引号。使用转义字符。