Add content-type default headers with application/json

This commit is contained in:
Honza Král
2017-02-08 20:47:57 +01:00
parent c7441e9dc0
commit d862c8a799
7 changed files with 22 additions and 7 deletions
+7
View File
@@ -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)
------------------
+1 -1
View File
@@ -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))
+2 -1
View File
@@ -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)
+1
View File
@@ -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 = {}
+1 -1
View File
@@ -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))
+9 -3
View File
@@ -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):
@@ -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": "привет"}')