Skip to content

400 BadRequestError on requests to local Elastic #2927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AidanStrong opened this issue Apr 16, 2025 · 2 comments
Closed

400 BadRequestError on requests to local Elastic #2927

AidanStrong opened this issue Apr 16, 2025 · 2 comments

Comments

@AidanStrong
Copy link

AidanStrong commented Apr 16, 2025

This issue exists on version 9.0.0 of the elasticsearch-py package. It does not exist on version 8.16.0.

It is replicated on python 3.10.12 and python 3.12.3.

In the python shell:

>>> import elasticsearch
>>> es = elasticsearch.Elasticsearch("http://localhost:9200")
>>> es.indices.get_alias(index="*")

Get's the error:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/aidans/.local/lib/python3.10/site-packages/elasticsearch/_sync/client/utils.py", line 415, in wrapped
    return api(*args, **kwargs)
  File "/home/aidans/.local/lib/python3.10/site-packages/elasticsearch/_sync/client/indices.py", line 2278, in get_alias
    return self.perform_request(  # type: ignore[return-value]
  File "/home/aidans/.local/lib/python3.10/site-packages/elasticsearch/_sync/client/_base.py", line 422, in perform_request
    return self._client.perform_request(
  File "/home/aidans/.local/lib/python3.10/site-packages/elasticsearch/_sync/client/_base.py", line 271, in perform_request
    response = self._perform_request(
  File "/home/aidans/.local/lib/python3.10/site-packages/elasticsearch/_sync/client/_base.py", line 351, in _perform_request
    raise HTTP_EXCEPTIONS.get(meta.status, ApiError)(
elasticsearch.BadRequestError: BadRequestError(400, 'media_type_header_exception', 'Invalid media-type value on headers [Content-Type, Accept]')

My local elastic is configured in docker and reachable with curl. The docker image is: docker.elastic.co/elasticsearch/elasticsearch:8.14.1

The only environment variables I configure the instance with are:

discovery.type: single-node
xpack.security.enabled: false
@miguelgrinberg
Copy link
Contributor

miguelgrinberg commented Apr 16, 2025

Versions 9.x of the elasticsearch client are not compatible with 8.x servers. You need to downgrade your client to 8.18.0, or else upgrade your server to 9.0.0.

In general, I would recommend that you use the same major and minor versions in your server and client, to ensure maximum compatibility. So in this case if you must stay on the 8.14.1 server you should use the latest 8.14.x version of the Python client package.

@pquentin
Copy link
Member

As of 9.0.1, elasticsearch-py will return a much clearer error:

elasticsearch.BadRequestError: BadRequestError(400, 'media_type_header_exception', 'Invalid media-type value on headers [Accept, Content-Type]', Accept version must be either version 8 or 7, but found 9. Accept=application/vnd.elasticsearch+json; compatible-with=9)

We may improve it in the future.

Additionally, we updated the release notes to clarify that the Elasticsearch server should be upgraded before the client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants