FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(net): preserve custom content type regardless of header casing by HarshRajSinghania · Pull Request #97 · pythonnative/pythonnative · GitHub

Repository navigation

fix(net): preserve custom content type regardless of header casing - #97

Merged
owenthcarey merged 3 commits into
pythonnative:mainfrom
HarshRajSinghania:fix/net-preserve-content-type-casing
Sep 12, 2026
Merged

owenthcarey merged 3 commits into
pythonnative:mainfrom
HarshRajSinghania:fix/net-preserve-content-type-casing

Conversation

Copy link
Copy Markdown
Contributor

Summary

_build_request() used header_dict.setdefault("Content-Type", "application/json") when encoding a mapping body. That lookup is case-sensitive, so a caller-supplied content-type / CONTENT-TYPE header was ignored and urllib.request.Request later collapsed both names, overwriting the custom value with application/json.

This change only adds the JSON default when no existing header name matches content-type case-insensitively. The caller mapping is copied first, so it is left unchanged.

Fixes #87

Motivation

Issue #87 documents that headers={"content-type": "application/merge-patch+json"} with a dict body produced Content-type: application/json on the request. That can change how an API interprets the payload.

Implementation

In src/pythonnative/net.py, replace setdefault("Content-Type", ...) with:

if not any(name.lower() == "content-type" for name in header_dict):
    header_dict["Content-Type"] = "application/json"

JSON encoding of mapping bodies is unchanged.

Testing

Added parameterized tests in tests/test_net.py that construct requests with _build_request() (no network):

  • Content-Type, content-type, CONTENT-TYPE, and Content-type all keep application/merge-patch+json
  • exactly one content-type header remains on the request
  • the original headers mapping is unchanged
  • the body still decodes to the original JSON object
  • a mapping body with no content-type still gets application/json

Checks performed in this environment:

  • Isolated reproduction of _build_request header assembly on Python 3.12 (project requires 3.13 for the full package import). Result: all four casings preserved a single application/merge-patch+json header; default path still sets application/json.
  • Full uv run pytest tests/test_net.py -q and ./scripts/check.sh were not run here because Python 3.13 is not available on this runner. Please rely on CI for those.

owenthcarey merged commit d902e5e into pythonnative:main Sep 12, 2026
28 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Preserve custom Content-Type headers when encoding a JSON request body

2 participants


Back | FazBrowse Home | New Git URL