有谁知道如何解决这个错误?
我用Golang向elasticsearch中插入数据,但是好像因为这个错误没有插入数据。
{"error":"Content-Type header [] is not supported","status":406}
我已经设置了内容类型。注意我用的是elasticsearch 6.4.3
request, err := http.NewRequest("POST", urlSearch, bytes.NewBuffer(query))
request.Close = true
request.Header.Set("Content-Type", "application/json")
最后但同样重要的是,我使用 elastigo 包向 elasticsearch 发出请求。
最佳答案
这是一个奇怪的响应,因为它暗示这一行:
request.Header.Set("Content-Type", "application/json")
无法将值添加到键 slice 。在现代围棋中不会发生这种情况,例如
data := []byte(`{"a":1}`)
req, err := http.NewRequest("POST", "", bytes.NewBuffer(data))
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Foo", "Bar")
fmt.Printf("%v\n", req.Header)
打印
map[Foo:[Bar]]
参见 go playground .
您是否正在使用与该行为不匹配的旧版 Go? (我在本地使用 1.11.2。)
五个建议:
(1) 处理 NewRequest 的 err 返回值以验证那里没有问题(参见上面的示例)。
(2) 在发送之前打印请求 Header 值以验证它在那个点看起来正确(参见上面的示例)。
(3) 试试the Add method对于 Content-Type header 而不是 Set 作为替代:
func (h Header) Add(key, value string)
(4) 确认您没有通过剥离 header 值的代理。
(5) 验证“application/json”是您正在访问的端点可接受的内容类型,因为错误响应中的空值本身可能是错误的。
祝你好运!
关于elasticsearch - {"error":"Content-Type header [] is not supported","status":406} When Inserting Data to Elasticsearch with Golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53317602/