Content-type header 不支持

Content-type header not supported

我正在关注这个 link for elasticsearch。

https://www.elastic.co/blog/a-practical-introduction-to-elasticsearch

我正在尝试跟随 curl 到 post json 数据。

curl -XPOST "http://localhost:9200/shakespeare/_bulk?pretty" --data-binary @D:\data\shakespeare.json

但我收到如下错误:

{
  "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
  "status" : 406
}

您需要将 header 中的内容类型设置为 application/json:

curl -XPOST -H "Content-Type: application/json" "http://localhost:9200/shakespeare/_bulk?pretty" --data-binary @D:\data\shakespeare.json

我从旧版本更新到 6.x.x 后收到同样的错误。我没有直接使用 curl 语句,我使用的是 python 的 elasticsearch 插件。

在我的例子中,我需要安装与更新后的 elasticsearch 服务器相对应的更新版本的插件。

pip install elasticsearch==6.3.1

确保你 运行 它与你的代码在相同的 Python 环境中执行。

希望这能让一些人头疼。

我找到了一个解决方案,您可以设置一个默认 header 来覆盖之前的 header:

RestClient restClient = RestClient
    .builder(new HttpHost(url, port, scheme))
    .setDefaultHeaders(new Header[]{
        new BasicHeader("Content-type", "application/json")
    })
    .build();