Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions elastic_transport/_async_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,11 @@ async def perform_request( # type: ignore[override, return]
+ resolve_default(client_meta, ())
)

request_headers.setdefault("content-type", "application/json")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generated apis already set content-type: application/json or x-ndjson for e.g. bulk requests before calling transport.

so setdefault is a no-op in this case, so the predefined content-type is preserved.

eg. bulk request in client where content-type is always set
https://github.com/elastic/elasticsearch-py/blob/c485ee48177892efd8083553ebf14bfaa804e59b/elasticsearch/_sync/client/__init__.py#L749-L752

e.g. where content-type is optionally set if body is present like search
https://github.com/elastic/elasticsearch-py/blob/f59037ee0e9de82719dce6ba8b56b9022d60ee0c/elasticsearch/_sync/client/watcher.py#L337-L341


# Serialize the request body to bytes based on the given mimetype.
request_body: Optional[bytes]
if body is not None:
if "content-type" not in request_headers:
raise ValueError(
"Must provide a 'Content-Type' header to requests with bodies"
)
request_body = self.serializers.dumps(
body, mimetype=request_headers["content-type"]
)
Expand Down
6 changes: 2 additions & 4 deletions elastic_transport/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,11 @@ def perform_request( # type: ignore[return]
+ resolve_default(client_meta, ())
)

request_headers.setdefault("content-type", "application/json")

# Serialize the request body to bytes based on the given mimetype.
request_body: Optional[bytes]
if body is not None:
if "content-type" not in request_headers:
raise ValueError(
"Must provide a 'Content-Type' header to requests with bodies"
)
request_body = self.serializers.dumps(
body, mimetype=request_headers["content-type"]
)
Expand Down
6 changes: 5 additions & 1 deletion tests/async_/test_async_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async def test_async_transport_httpbin(httpbin_node_config, httpbin):
assert data["headers"] == {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/json",
"User-Agent": DEFAULT_USER_AGENT,
"Connection": "keep-alive",
"Host": f"{httpbin.host}:{httpbin.port}",
Expand Down Expand Up @@ -93,7 +94,10 @@ async def test_request_with_custom_user_agent_header():
assert {
"body": None,
"request_timeout": DEFAULT,
"headers": {"user-agent": "my-custom-value/1.2.3"},
"headers": {
"user-agent": "my-custom-value/1.2.3",
"content-type": "application/json",
},
} == t.node_pool.get().calls[0][1]


Expand Down
11 changes: 8 additions & 3 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def test_request_with_custom_user_agent_header():
assert {
"body": None,
"request_timeout": DEFAULT,
"headers": {"user-agent": "my-custom-value/1.2.3"},
"headers": {
"user-agent": "my-custom-value/1.2.3",
"content-type": "application/json",
},
} == t.node_pool.get().calls[0][1]


Expand Down Expand Up @@ -402,7 +405,8 @@ class DummyNodeWithClientMeta(DummyNode):
assert 1 == len(calls)
headers = calls[0][1]["headers"]

assert sorted(headers.keys()) == ["x-elastic-client-meta"]
assert sorted(headers.keys()) == ["content-type", "x-elastic-client-meta"]
assert headers["content-type"] == "application/json"
assert re.match(
r"^es=8\.0\.0p,py=[0-9.]+p?,t=[0-9.]+p?,dm=0\.0\.0p$",
headers["x-elastic-client-meta"],
Expand All @@ -424,7 +428,8 @@ class DummyNodeWithClientMeta(DummyNode):
assert 1 == len(calls)
headers = calls[0][1]["headers"]

assert sorted(headers.keys()) == ["x-elastic-client-meta"]
assert sorted(headers.keys()) == ["content-type", "x-elastic-client-meta"]
assert headers["content-type"] == "application/json"
assert re.match(
r"^es=8\.0\.0p,py=[0-9.]+p?,t=[0-9.]+p?,dm=0\.0\.0p,h=s$",
headers["x-elastic-client-meta"],
Expand Down