问题一:Content-Type header is not supported

问题描述


curl -s -XPOST "localhost:9200/get-together/group/1" -d'{

"name": "Denver Clojure",

"organizer": ["Daniel", "Lee"],

"description": "Group of Clojure enthusiasts from Denver who want to hack on code together and learn more about Clojure",

"created_on": "2012-06-15",

"tags": ["clojure", "denver", "functional programming", "jvm", "java"],

"members": ["Lee", "Daniel", "Mike"],

"location_group": "Denver, Colorado, USA"

}'

在执行上述创建索引的时候,报如下错误:


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

问题原因

strict-content-type-checking-for-elasticsearch-rest-requests

是因为在 ES6.0 之后,ES 对 content-type 的检测更为严格,在 ES 的早期版本中,content-type 是可选的,如果缺省或者 ES 无法辨别,ES 会根据请求内容进行猜测。

这个功能最先出现在 5.3 版本,http.content_type.required 是在配置中的,在 5.x 版本中,默认参数是 false,但是在 6.0 版本中,这个参数是 true,并且不能改变。

为什么要对这个做出改变呢,是因为随着 ES 的发展,ES 认为可靠性和可预测性更重要,猜测一定会猜错,但是增加了一点点内容,有助于安全和清晰,这是非常明智的。

解决方案

在请求时增加 content-type 类型


curl -H 'Content-Type: application/json' -s -XPOST "localhost:9200/get-together/group/1" -d'{

"name": "Denver Clojure",

"organizer": ["Daniel", "Lee"],

"description": "Group of Clojure enthusiasts from Denver who want to hack on code together and learn more about Clojure",

"created_on": "2012-06-15",

"tags": ["clojure", "denver", "functional programming", "jvm", "java"],

"members": ["Lee", "Daniel", "Mike"],

"location_group": "Denver, Colorado, USA"

}'
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!