Skip to content

Commit

Permalink
Fix #395, properly update the content-type header
Browse files Browse the repository at this point in the history
  • Loading branch information
lexiforest committed Oct 4, 2024
1 parent 137a834 commit a2b6f98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 10 additions & 6 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,17 @@ def _update_url_params(url: str, *params_list: Union[Dict, List, Tuple, None]) -
return new_url


def _update_header_line(header_lines: List[str], key: str, value: str, force: bool = False):
# TODO: should we move this function to headers.py?
def _update_header_line(header_lines: List[str], key: str, value: str, replace: bool = False):
"""Update header line list by key value pair."""
found = False
for idx, line in enumerate(header_lines):
if line.lower().startswith(key.lower() + ":") and force:
header_lines[idx] = f"{key}: {value}"
if line.lower().startswith(key.lower() + ":"):
found = True
if replace:
header_lines[idx] = f"{key}: {value}"
break
else: # if not break
if not found:
header_lines.append(f"{key}: {value}")


Expand Down Expand Up @@ -449,14 +453,14 @@ def _set_curl_options(

# Add content-type if missing
if json is not None:
_update_header_line(header_lines, "Content-Type", "application/json", force=True)
_update_header_line(header_lines, "Content-Type", "application/json", replace=True)
if isinstance(data, dict) and method != "POST":
_update_header_line(header_lines, "Content-Type", "application/x-www-form-urlencoded")
if isinstance(data, (str, bytes)):
_update_header_line(header_lines, "Content-Type", "application/octet-stream")

# Never send `Expect` header.
_update_header_line(header_lines, "Expect", "", force=True)
_update_header_line(header_lines, "Expect", "", replace=True)

c.setopt(CurlOpt.HTTPHEADER, [h.encode() for h in header_lines])

Expand Down
13 changes: 13 additions & 0 deletions tests/unittest/test_headers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from curl_cffi.requests import Headers
from curl_cffi.requests.session import _update_header_line


def test_headers():
Expand All @@ -21,3 +22,15 @@ def test_header_output():
headers = Headers({"X-Foo": "bar"})
header_list = headers.multi_items()
assert header_list[0][0] == "X-Foo"


def test_replace_header():
header_lines = []
_update_header_line(header_lines, "content-type", "image/png")
assert header_lines == ["content-type: image/png"]
_update_header_line(header_lines, "Content-Type", "application/json")
assert header_lines == ["content-type: image/png"]
_update_header_line(header_lines, "Content-Type", "application/json", replace=True)
assert header_lines == ["Content-Type: application/json"]
_update_header_line(header_lines, "Host", "example.com", replace=True)
assert header_lines == ["Content-Type: application/json", "Host: example.com"]

0 comments on commit a2b6f98

Please sign in to comment.