Skip to content
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

How to modify http headers sent with an elasticsearch request? #407

Closed
ApproximateIdentity opened this issue May 19, 2016 · 5 comments
Closed

Comments

@ApproximateIdentity
Copy link

I've searched the documentation/internet and cannot find an answer to this question. How do I pass in extra headers to http requests in say methods like elasticsearch.Elasticsearch.search? Well really it would be most convenient if I could just do it once at say instantiation of elasticsearch.Elasticsearch...

How is this most easily achieved?

@honzakral
Copy link
Contributor

Currently there is no direct way to inject your own headers. What I recommend is to subclass the connection class and modify the headers there:

from elasticsearch import Urllib3HttpConnection, Elasticsearch
class MyConnection(Urllib3HttpConnection):
    def __init__(*args, **kwargs):
        extra_headers = kwargs.pop('extra_headers', {})
        super(MyConnection, self).__init__(*args, **kwargs)
        self.headers.update(extra_headers)

es = Elasticsearch(connection_class=MyConnection, extra_headers={'Any': 'header'})

Unfortunately there is no easy way to do this on a per-api basis.

Hope this helps.

@ApproximateIdentity
Copy link
Author

Thanks for the response! This method will probably be fine. Cheers!

@gibbon88
Copy link

NameError: global name 'self' is not defined

@honzakral
Copy link
Contributor

@gibbon88 yes, I forgot the self argument in def __init__(self, *args, **kwargs)

@sfariaNG
Copy link

sfariaNG commented Sep 20, 2019

Thanks for the snippet @honzakral For anyone looking at this in future, I modified his code to allow sending a custom header with a value set at request time:

from elasticsearch import Urllib3HttpConnection

class MyUrllib3Http(Urllib3HttpConnection):
    def __init__(self, *args, **kwargs):
        internal_params = kwargs.pop("internal_params", {})
        super(MyUrllib3Http, self).__init__(*args, **kwargs)
        self.internal_params = internal_params

    def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=(), headers=None):
        for param,header in self.internal_params.items():
          if param in params:
            headers = {} if headers is None else headers
            headers[header] = params.pop(param)

        return super().perform_request(method, url, params=params, body=body, timeout=timeout, ignore=ignore, headers=headers)

internal_params is a dict whose keys are the param keys in params and values are the header their value should be placed in.

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

4 participants