question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to modify http headers sent with an elasticsearch request?

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
honzakralcommented, May 19, 2016

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.

4reactions
sfariaNGcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP/REST clients and security | Elasticsearch Guide [master]
Some APIs support secondary authorization headers for situations where you want tasks to run with a different set of credentials. For example, you...
Read more >
Send Headers in HTTP post request - Beats - Elastic Discuss
Hi All, I am not able to send additional headers in the http POST request. yml file heartbeat.monitors: - type: http # List...
Read more >
API conventions | Elasticsearch Guide [8.5] | Elastic
Elasticsearch ignores any other encoding headings sent with a request. Responses are also UTF-8 encoded. X-Opaque-Id HTTP headeredit. You can pass an X-Opaque- ......
Read more >
Strict Content-Type Checking for Elasticsearch REST Requests
This ability to enforce strict content-type checking has existed since Elasticsearch 5.3 via the http.content_type.required configuration ...
Read more >
Performing requests | Elasticsearch Java API Client [7.17]
addHeader is for headers that are required for authorization or to work with a proxy in front of Elasticsearch. There is no need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found