Skip to content

fix: set Content-Type to application/fhir+json for PATCH requests (Fixes #155)#156

Open
rtmalikian wants to merge 1 commit into
beda-software:masterfrom
rtmalikian:fix/issue-138-patch-content-type
Open

fix: set Content-Type to application/fhir+json for PATCH requests (Fixes #155)#156
rtmalikian wants to merge 1 commit into
beda-software:masterfrom
rtmalikian:fix/issue-138-patch-content-type

Conversation

@rtmalikian

Copy link
Copy Markdown

Fixes #155

Summary

PATCH operations fail against HAPI FHIR servers with:

HAPI-1965: Invalid Content-Type for PATCH operation: application/json

The _do_request method uses requests.request(method, url, json=data) which automatically sets Content-Type: application/json. HAPI FHIR rejects this for PATCH operations per the FHIR specification.

Solution

Set Content-Type: application/fhir+json for PATCH requests in both sync (lib_sync.py) and async (lib_async.py) _do_request implementations. This matches the FHIR specification requirement that PATCH requests use application/fhir+json content type.

The fix is applied at the _do_request level so it covers all PATCH code paths:

  • client.patch() — direct PATCH by resource type + ID
  • resource.save(fields=[...]) — partial update via field selection (uses PATCH internally)
  • Conditional PATCH via search sets

Verification

  • Syntax check: python3 -c "import ast; ast.parse(open('fhirpy/base/lib_sync.py').read())" — passes
  • Ruff lint: no new warnings introduced (all 8 warnings are pre-existing)
  • Both sync and async implementations fixed symmetrically

Changelog

Date Change Author
2026-06-19 Set Content-Type to application/fhir+json for PATCH requests rtmalikian

Files Changed

  • fhirpy/base/lib_sync.py — Add Content-Type header for PATCH in SyncClient._do_request
  • fhirpy/base/lib_async.py — Add Content-Type header for PATCH in AsyncClient._do_request

Verification

  • Syntax check passed for both modified files
  • Ruff lint: no new warnings introduced
  • Fix covers all PATCH code paths (client.patch(), resource.save(fields=[...]), conditional patch)

About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.

📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a


Disclosure: This code was developed with assistance from mimo-v2.5-pro (xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.

HAPI FHIR rejects PATCH requests with Content-Type: application/json
(HAPI-1965). Set Content-Type to application/fhir+json when the HTTP
method is PATCH, matching the FHIR specification requirement.

Fixes beda-software#155

Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
@rtmalikian

Copy link
Copy Markdown
Author

Friendly ping — this PR is ready for review when you have a chance. Happy to address any feedback!

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.

fix: set Content-Type to application/fhir+json for PATCH requests

1 participant