Skip to content

fix(python): keep a custom Content-Type when sending form data - #583

Merged
barjin merged 1 commit into
apify:masterfrom
RaphaelFakhri:fix/python-form-content-type
Oct 19, 2026
Merged

barjin merged 1 commit into
apify:masterfrom
RaphaelFakhri:fix/python-form-content-type

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

When you pass a dict to data=, the Python client sets Content-Type: application/x-www-form-urlencoded on the request. It does this unconditionally, so a Content-Type header you pass in headers= gets replaced, for example application/x-www-form-urlencoded; charset=utf-8. If your header key uses a different case, such as content-type, both entries end up in the request header map, and which one is sent depends on the hash map iteration order.

This change adds the default Content-Type only when the request headers don't already contain one (compared case-insensitively). This matches httpx, where an explicit Content-Type takes precedence over the one derived from data=.

Changes

  • impit-python/src/request.rs: in to_body, skip the default form Content-Type if the caller already set the header.
  • impit-python/test/basic_client_test.py, impit-python/test/async_client_test.py: add test_form_keeps_custom_content_type. The tests send a form with a custom Content-Type to the local echoing server and check the header that arrives.

Testing

  • Without the fix, both new tests fail: the echoed request contains content-type: application/x-www-form-urlencoded without the charset parameter.
  • With the fix, both new tests pass.

Fixes #582

@barjin barjin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm, thank you @RaphaelFakhri!

@barjin
barjin merged commit c1589ad into apify:master Oct 19, 2026
23 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(py) Form data= overwrites a custom Content-Type header

3 participants