You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch 6.0请求报406错误:Content-Type不支持,Java客户端问题求解

Fixing Elasticsearch 6.0 406 Error ("Content-Type header [text/plain] is not supported")

Hey there, let's break down why you're hitting this 406 error and fix it for both general requests and your Java 1.6 client.

What's causing the error?

Starting with Elasticsearch 6.0, the team added stricter validation for request content types. Any request that sends JSON data (like your search DSL) must explicitly declare Content-Type: application/json in the request headers. If you don't set this, Elasticsearch assumes the content is text/plain, which it no longer supports for JSON payloads—hence the 406 "not supported" error.

Solution for your Java 1.6 client

Looking at your code, you've set .accept("application/json") which tells Elasticsearch you want a JSON response, but you haven't specified what type of data you're sending to Elasticsearch. That's the missing piece!

You need to add .type("application/json") to your WebResource call to declare that your DSL payload is JSON. Here's the corrected code:

Client clientInstance = Client.create();
clientInstance.addFilter(new HTTPBasicAuthFilter("", ""));
WebResource webResource = clientInstance.resource("http://hostname:9200/indexname/_search");
// Add .type("application/json") to define the request content type
ClientResponse response = webResource.entity(dsl)
                                    .type("application/json")
                                    .accept("application/json")
                                    .get(ClientResponse.class);
String result = response.getEntity(String.class);

Why does Kibana work but your Java client doesn't?

Kibana automatically injects the correct Content-Type: application/json header into all its search requests, so Elasticsearch accepts the DSL without issue. Your Java client wasn't doing this, which is why it threw the error even though the DSL itself is valid.

Quick fix for other tools (e.g., curl)

If you ever hit this error with curl or similar tools, just add the Content-Type header explicitly:

curl -H "Content-Type: application/json" -X GET http://hostname:9200/indexname/_search -d '{"query": {"match_all": {}}}'

That should resolve the 406 error across both scenarios. Let me know if you run into any other hiccups!

内容的提问来源于stack exchange,提问作者Elva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:38:50