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

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

我已经将Elasticsearch(5.5版)集成到Gitlab中并尝试使用它。这是我从外部 windows 客户端发送的命令:

curl -XGET gitlab.server:9200/ -H 'Content-Type: application/json' -d '{"query": {"simple_query_string" : {"fields" : ["content"], "query" : "foo bar -baz"}}}'

但它不起作用。在客户端我得到这些错误:

{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}
curl: (6) Could not resolve host: text
curl: (3) [globbing] unmatched brace in column 1
curl: (3) Bad URL, colon is first character
curl: (3) [globbing] unmatched brace in column 1
curl: (3) Bad URL, colon is first character
curl: (3) [globbing] bad range in column 2
curl: (6) Could not resolve host: query
curl: (3) Bad URL, colon is first character
curl: (3) [globbing] unmatched close brace/bracket in column 13

在 /var/log/elasticsearch/elasticsearch.log 中的服务器上,我看不到任何日志消息。

但是,运行 来自 linux 服务器的完全相同的命令给我一个没有错误的响应:

{
  "name" : "name",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "uuid",
  "version" : {
    "number" : "5.5.0",
    "build_hash" : "260387d",
    "build_date" : "2017-06-30T23:16:05.735Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}

我尝试将 http.content_type.required: true 添加到 elasticsearch.yml,但问题是一样的。那么,我在这里做错了什么?为什么我从 Windows 客户端得到一个 "Content-Type header not supported"?我该如何解决这个问题?

像这样将 ' 更改为 " 后:

curl -XGET gitlab.server:9200/ -H "Content-Type: application/json" -d "{"query": {"simple_query_string" : {"fields" : ["content"], "query" : "foo bar -baz"}}}"

我收到了这个回复:

{
  "name" : "name",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "uuid",
  "version" : {
    "number" : "5.5.0",
    "build_hash" : "260387d",
    "build_date" : "2017-06-30T23:16:05.735Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}
curl: (6) Could not resolve host: bar

将引号从 ' 更改为 " 后,将参数中使用的引号 " 转义如下:

curl -XGET gitlab.server:9200/ -H "Content-Type: application/json" -d "{\"query\": {\"simple_query_string\" : {\"fields\" : [\"content\"], \"query\" : \"foo bar -baz\"}}}"

一个alternative就是把json放到一个文件中,参数使用@前缀。

json.txt

{
  "query": {
    "simple_query_string" : { 
      "fields" : ["content"], 
      "query" : "foo bar -baz"
    }
  }
}

和运行卷曲如下:

curl -XGET gitlab.server:9200/ -H "Content-Type: application/json" -d @json.txt