From d862c8a799e485649da3beaef7037860d01ac3e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Wed, 8 Feb 2017 20:47:57 +0100 Subject: [PATCH] Add content-type default headers with `application/json` --- Changelog.rst | 7 +++++++ elasticsearch/__init__.py | 2 +- elasticsearch/connection/http_requests.py | 3 ++- elasticsearch/connection/http_urllib3.py | 1 + setup.py | 2 +- test_elasticsearch/test_connection.py | 12 +++++++++--- test_elasticsearch/test_server/test_client.py | 2 +- 7 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Changelog.rst b/Changelog.rst index 82b00b85..0ce112e8 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -3,6 +3,13 @@ Changelog ========= +5.2.0 (dev) +----------- + +The client now automatically sends ``Content-Type`` http header set to +``application/json``. If you are explicitly passing in other encoding than +``json`` you need to set the header manually. + 5.1.0 (2017-01-11) ------------------ diff --git a/elasticsearch/__init__.py b/elasticsearch/__init__.py index 8f932b41..bb69be0e 100644 --- a/elasticsearch/__init__.py +++ b/elasticsearch/__init__.py @@ -1,6 +1,6 @@ from __future__ import absolute_import -VERSION = (5, 1, 0) +VERSION = (5, 2, 0, 'dev') __version__ = VERSION __versionstr__ = '.'.join(map(str, VERSION)) diff --git a/elasticsearch/connection/http_requests.py b/elasticsearch/connection/http_requests.py index ccd0be2b..7f4bbe7f 100644 --- a/elasticsearch/connection/http_requests.py +++ b/elasticsearch/connection/http_requests.py @@ -34,7 +34,8 @@ class RequestsHttpConnection(Connection): super(RequestsHttpConnection, self).__init__(host=host, port=port, **kwargs) self.session = requests.Session() - self.session.headers = headers + self.session.headers = headers or {} + self.session.headers.setdefault('content-type', 'application/json') if http_auth is not None: if isinstance(http_auth, (tuple, list)): http_auth = tuple(http_auth) diff --git a/elasticsearch/connection/http_urllib3.py b/elasticsearch/connection/http_urllib3.py index eb8eda56..7b4e6c79 100644 --- a/elasticsearch/connection/http_urllib3.py +++ b/elasticsearch/connection/http_urllib3.py @@ -61,6 +61,7 @@ class Urllib3HttpConnection(Connection): for k in headers: self.headers[k.lower()] = headers[k] + self.headers.setdefault('content-type', 'application/json') ca_certs = CA_CERTS if ca_certs is None else ca_certs pool_class = urllib3.HTTPConnectionPool kw = {} diff --git a/setup.py b/setup.py index 4712c117..f240a62c 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from os.path import join, dirname from setuptools import setup, find_packages import sys -VERSION = (5, 1, 0) +VERSION = (5, 2, 0. 'dev') __version__ = VERSION __versionstr__ = '.'.join(map(str, VERSION)) diff --git a/test_elasticsearch/test_connection.py b/test_elasticsearch/test_connection.py index 540623e1..9282e1e1 100644 --- a/test_elasticsearch/test_connection.py +++ b/test_elasticsearch/test_connection.py @@ -18,21 +18,27 @@ class TestUrllib3Connection(TestCase): def test_keep_alive_is_on_by_default(self): con = Urllib3HttpConnection() - self.assertEquals({'connection': 'keep-alive'}, con.headers) + self.assertEquals({'connection': 'keep-alive', + 'content-type': 'application/json'}, con.headers) def test_http_auth(self): con = Urllib3HttpConnection(http_auth='username:secret') - self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0', - 'connection': 'keep-alive'}, con.headers) + self.assertEquals({ + 'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0', + 'connection': 'keep-alive', + 'content-type': 'application/json' + }, con.headers) def test_http_auth_tuple(self): con = Urllib3HttpConnection(http_auth=('username', 'secret')) self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0', + 'content-type': 'application/json', 'connection': 'keep-alive'}, con.headers) def test_http_auth_list(self): con = Urllib3HttpConnection(http_auth=['username', 'secret']) self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0', + 'content-type': 'application/json', 'connection': 'keep-alive'}, con.headers) def test_uses_https_if_verify_certs_is_off(self): diff --git a/test_elasticsearch/test_server/test_client.py b/test_elasticsearch/test_server/test_client.py index cbb419e8..70393fd6 100644 --- a/test_elasticsearch/test_server/test_client.py +++ b/test_elasticsearch/test_server/test_client.py @@ -5,4 +5,4 @@ from . import ElasticsearchTestCase class TestUnicode(ElasticsearchTestCase): def test_indices_analyze(self): - self.client.indices.analyze(body='привет') + self.client.indices.analyze(body='{"text": "привет"}')