【问题标题】:CURL : Content-Type header [application/x-www-form-urlencoded] is not supportedCURL:不支持 Content-Type 标头 [application/x-www-form-urlencoded]
【发布时间】:2019-05-24 11:56:38
【问题描述】:

使用 ElasticSearch 并尝试一些查询以创建索引,使用 curl 发布数据。

使用 GIT 提供的 curl(Windows GIT)

该命令用于将文档添加到名为 customer 的索引中。

来自 ElasticSearch 站点的 curl 命令复制如下:

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'

上面的命令对我不起作用。我只是把它做成如下一行

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d '{"name": "John Doe"}'

我收到以下错误。

其他命令,如创建索引,如下所示

curl -X PUT "localhost:9200/customer?pretty"

Response is :

{
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "customer"
    }

以 json 为内容的 curl 命令不起作用。

已经参考了以下链接,但无法获取 Content Type Issue

【问题讨论】:

    标签: elasticsearch curl


    【解决方案1】:

    在 Windows 上,您需要使用双引号并将内容中的引号转义:

    curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H "Content-Type: application/json" -d "{\"name\": \"John Doe\"}"
    

    或者,您可以将内容存储在名为 data.json 的文件中

    {"name": "John Doe"}
    

    然后像这样通过 curl 发送它,这样您就不必转义双引号:

    curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H "Content-Type: application/json" --data-binary @data.json
    

    【讨论】: