Skip to content

Commit 311ac7c

Browse files
committed
Allow specifying custom http headers
1 parent ac74778 commit 311ac7c

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

Changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Changelog
88

99
Version compatible with elasticsearch 5.0
1010

11+
* added ``headers`` arg to connections to support custom http headers
12+
1113
2.3.0 (2016-02-29)
1214
------------------
1315

elasticsearch/connection/http_requests.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ class RequestsHttpConnection(Connection):
2424
certificate, or cert only if using client_key
2525
:arg client_key: path to the file containing the private key if using
2626
separate cert and key files (client_cert will contain only the cert)
27+
:arg headers: any custom http headers to be add to requests
2728
"""
2829
def __init__(self, host='localhost', port=9200, http_auth=None,
2930
use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None,
30-
client_key=None, **kwargs):
31+
client_key=None, headers=None, **kwargs):
3132
if not REQUESTS_AVAILABLE:
3233
raise ImproperlyConfigured("Please install requests to use RequestsHttpConnection.")
3334

3435
super(RequestsHttpConnection, self).__init__(host=host, port=port, **kwargs)
35-
self.session = requests.session()
36+
self.session = requests.Session()
37+
self.session.headers = headers
3638
if http_auth is not None:
3739
if isinstance(http_auth, (tuple, list)):
3840
http_auth = tuple(http_auth)

elasticsearch/connection/http_urllib3.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ class Urllib3HttpConnection(Connection):
3333
:arg ssl_assert_fingerprint: verify the supplied certificate fingerprint if not `None`
3434
:arg maxsize: the maximum number of connections which will be kept open to
3535
this host.
36+
:arg headers: any custom http headers to be add to requests
3637
"""
3738
def __init__(self, host='localhost', port=9200, http_auth=None,
3839
use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None,
3940
client_key=None, ssl_version=None, ssl_assert_hostname=None,
40-
ssl_assert_fingerprint=None, maxsize=10, **kwargs):
41+
ssl_assert_fingerprint=None, maxsize=10, headers=None, **kwargs):
4142

4243
super(Urllib3HttpConnection, self).__init__(host=host, port=port, use_ssl=use_ssl, **kwargs)
43-
self.headers = urllib3.make_headers(keep_alive=True)
44+
self.headers = headers.copy() if headers else {}
45+
self.headers.update(urllib3.make_headers(keep_alive=True))
4446
if http_auth is not None:
4547
if isinstance(http_auth, (tuple, list)):
4648
http_auth = ':'.join(http_auth)

0 commit comments

Comments
 (0)