【问题标题】:{"error":"Content-Type header [] is not supported","status":406} When Inserting Data to Elasticsearch with Golang{"error":"Content-Type header [] is not supported","status":406} 使用 Golang 向 Elasticsearch 插入数据时
【发布时间】:2018-11-15 10:42:24
【问题描述】:

有谁知道如何解决这个错误?

我用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.Close = true”这一行后你试过了吗?
  • github.com/olivere/elastic 很容易用于向 Elasticsearch 发出请求,也许你可以看看,而不是编写自己的客户端
  • 错误提示“不支持 Content-Type 标头”。您是否尝试过不发送 Content-Type 标头?

标签: elasticsearch go header content-type


【解决方案1】:

这是一个奇怪的反应,因为它表明这一行:

request.Header.Set("Content-Type", "application/json")

未能将值添加到键切片。在现代围棋中不会发生这种情况,例如

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 代替 Set 作为 Content-Type 标头:

func (h Header) Add(key, value string)

(4) 确认您没有通过去除标头值的代理。

(5) 验证“application/json”是您所访问的端点可接受的内容类型,因为错误响应中的空值本身可能是错误的。

祝你好运!

【讨论】:

  • 谢谢您的回复,我已经尝试过您从1到3的选项。至于没有。 4,我不确定它与代理有关。对于 5 号,我不知道“application/json”是否是 elasticsearch 可接受的内容类型。我使用 elasticsearch 6.4.3,它不再接受字符串。相反,它使用文本替换字符串作为数据类型。你有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-28
  • 2017-04-12
  • 1970-01-01
  • 2019-08-16
  • 2021-06-01
  • 2022-12-02
  • 2017-09-04
相关资源
最近更新 更多