elastic/elasticsearch-py

Ability to pass headers to index function / other functions or Load headers from client

RishabhSood opened this issue · 1 comments

Describe the feature:
Hi Team, this line forces some headers being passed to the endpoint, can we please introduce support for passing custom headers with each request ?

__headers = {"accept": "application/json", "content-type": "application/json"}

Hello! Per-request headers are supported with the .options() API which creates a new Elasticsearch client and allows passing it various options, including headers. If you try the following example which enables debug logging:

from elasticsearch import Elasticsearch
import elastic_transport
elastic_transport.debug_logging()

es = Elasticsearch("http://localhost:9200")
es.index(index="test", document={"foo": "baz"})
es.options(headers={"custom": "header"}).index(index="test", document={"foo": "bar"})

You will see that the second request includes Custom: header while the first does not:

[2024-03-29T11:17:57] > POST /test/_doc HTTP/1.1
> Accept: application/vnd.elasticsearch+json; compatible-with=8
> Connection: keep-alive
> Content-Type: application/vnd.elasticsearch+json; compatible-with=8
> User-Agent: elasticsearch-py/8.13.0 (Python/3.12.2; elastic-transport/8.13.0)
> X-Elastic-Client-Meta: es=8.13.0,py=3.12.2,t=8.13.0,ur=2.2.1
> {"foo":"baz"}
< HTTP/1.1 201 Created
< Location: /test/_doc/zR8SiY4BfFLxlYtpiETx
< X-Elastic-Product: Elasticsearch
< Content-Length: 156
< Content-Type: application/vnd.elasticsearch+json;compatible-with=8
< {"_index":"test","_id":"zR8SiY4BfFLxlYtpiETx","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
[2024-03-29T11:17:57] POST http://localhost:9200/test/_doc [status:201 duration:0.353s]
[2024-03-29T11:17:57] > POST /test/_doc HTTP/1.1
> Accept: application/vnd.elasticsearch+json; compatible-with=8
> Connection: keep-alive
> Content-Type: application/vnd.elasticsearch+json; compatible-with=8
> Custom: header
> User-Agent: elasticsearch-py/8.13.0 (Python/3.12.2; elastic-transport/8.13.0)
> X-Elastic-Client-Meta: es=8.13.0,py=3.12.2,t=8.13.0,ur=2.2.1
> {"foo":"bar"}
< HTTP/1.1 201 Created
< Location: /test/_doc/zh8SiY4BfFLxlYtpiURU
< X-Elastic-Product: Elasticsearch
< Content-Length: 156
< Content-Type: application/vnd.elasticsearch+json;compatible-with=8
< {"_index":"test","_id":"zh8SiY4BfFLxlYtpiURU","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
[2024-03-29T11:17:57] POST http://localhost:9200/test/_doc [status:201 duration:0.009s]