Code Monkey home page Code Monkey logo

Comments (1)

pquentin avatar pquentin commented on June 7, 2024 1

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]

from elasticsearch-py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.