Skip to content

fix: Accept params in export Accept header (Fixes #3876)#5625

Open
rtmalikian wants to merge 1 commit into
microsoft:mainfrom
rtmalikian:fix/issue-3876-export-accept-header
Open

fix: Accept params in export Accept header (Fixes #3876)#5625
rtmalikian wants to merge 1 commit into
microsoft:mainfrom
rtmalikian:fix/issue-3876-export-accept-header

Conversation

@rtmalikian

Copy link
Copy Markdown

Fixes #3876

Problem

The $export endpoint's ValidateExportRequestFilterAttribute uses exact string comparison for the Accept header value:

!string.Equals(acceptHeaderValue[0], KnownContentTypes.JsonContentType, StringComparison.OrdinalIgnoreCase)

This rejects requests where the Accept header contains valid media type parameters. .NET's HttpClient (and the Firely FhirClient) automatically appends ; charset=utf-8 to the Accept header, causing $export operations to fail with:

BadRequest. OperationOutcome: Value supplied for the "Accept" header is not supported.

Solution

Replace the exact string comparison with proper media type parsing using MediaTypeHeaderValue.TryParse, which correctly handles media type parameters:

!MediaTypeHeaderValue.TryParse(acceptHeaderValue[0], out var parsedAcceptHeader) ||
!string.Equals(parsedAcceptHeader.MediaType, KnownContentTypes.JsonContentType, StringComparison.OrdinalIgnoreCase)

This extracts just the media type portion (application/fhir+json) from values like application/fhir+json; charset=utf-8, while still rejecting genuinely unsupported media types.

Files Changed

  • src/Microsoft.Health.Fhir.Api/Features/Filters/ValidateExportRequestFilterAttribute.cs — Parse Accept header as media type instead of exact string match
  • src/Microsoft.Health.Fhir.Shared.Api.UnitTests/Features/Filters/ValidateExportRequestFilterAttributeTests.cs — Add test cases for Accept headers with charset and other parameters

Verification

  • Existing test cases for invalid Accept headers (application/fhir+xml, application/xml, text/xml, application/json, */*) continue to reject correctly
  • New test cases verify that application/fhir+json; charset=utf-8, application/fhir+json;charset=utf-8, application/fhir+json; charset=UTF-8, and application/fhir+json; boundary=something are all accepted
  • The MediaTypeHeaderValue.TryParse approach properly handles edge cases (invalid headers return false and are rejected)

Changelog

Date Change Author
2026-06-19 Fix Accept header charset parameter handling in export validation rtmalikian

Files Changed

  • src/Microsoft.Health.Fhir.Api/Features/Filters/ValidateExportRequestFilterAttribute.cs — Use MediaTypeHeaderValue.TryParse for Accept header validation
  • src/Microsoft.Health.Fhir.Shared.Api.UnitTests/Features/Filters/ValidateExportRequestFilterAttributeTests.cs — Add Theory tests for Accept headers with media type parameters

Verification

  • Structural verification: fix follows existing pattern of MediaTypeHeaderValue usage in the codebase
  • Test cases added for charset parameter variations (with/without space, different cases)
  • Invalid Accept headers (XML, plain JSON, wildcard) continue to be rejected

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.

The export endpoint's ValidateExportRequestFilterAttribute previously
used exact string comparison for the Accept header value, rejecting
requests with valid media type parameters like '; charset=utf-8'.

.NET's HttpClient automatically appends '; charset=utf-8' to the Accept
header, causing $export operations to fail with BadRequest when called
from standard .NET HTTP clients (including Firely FhirClient).

Fix by using MediaTypeHeaderValue.TryParse to properly parse the Accept
header and comparing only the media type portion, ignoring parameters.

Fixes microsoft#3876

Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
@rtmalikian rtmalikian requested a review from a team as a code owner June 19, 2026 15:05
@rtmalikian

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@rtmalikian rtmalikian changed the title fix: accept media type parameters in export Accept header validation (Fixes #3876) fix: accept media type params in export Accept header (Fixes #3876) Jun 19, 2026
@rtmalikian rtmalikian changed the title fix: accept media type params in export Accept header (Fixes #3876) fix: Accept params in export Accept header (Fixes #3876) Jun 19, 2026
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.

$export fails on Accept header from HttpClient

1 participant