Home » Accept and Content-Type in APIs: Handling Unsupported Representations

Accept and Content-Type in APIs: Handling Unsupported Representations

A JSON request asks for an XML response through Accept; each message has its own Content-Type, and unsupported format and malformed content are separate branches.

In an HTTP API request, Content-Type describes the representation being sent, while Accept can express preferences for the response representation. Validate what the server receives separately from choosing what it returns. A request can contain valid JSON and still ask for a response format the endpoint does not provide.

The distinction becomes useful when an application stops treating every body as “some JSON” and starts publishing a stable contract for supported media types, parsing errors, and response negotiation.

Follow both directions of one exchange

For a synthetic ticket endpoint, a client might send:

POST /tickets HTTP/1.1
Content-Type: application/json
Accept: application/json, application/problem+json

{"subject":"Synthetic fixture"}

The body is identified as JSON. The Accept field allows the API's intended success representation and its documented Problem Details representation. A successful response should identify its own selected representation with Content-Type; it should not simply echo the request's field without inspecting the response it produced.

The MDN Content-Type reference explains the field's role in messages. The Accept reference describes preferences and negotiation, including media ranges and weighting.

Define the endpoint's supported set

Suppose this example accepts JSON request bodies, produces JSON success responses, and deliberately rejects an unacceptable requested response format instead of supplying a default. Its policy could distinguish:

Request situation Decision in this example API
Supported JSON body and an acceptable response type Process the operation under its other validation and authorization rules
XML body where XML is unsupported Reject the unsupported request representation
Valid JSON body but only an unsupported response type is acceptable Reject under the endpoint's explicit response-negotiation policy
Malformed JSON labeled as JSON Apply the documented parsing-error contract, rather than treating it as an unsupported media type

These are separate paths through the interface. A server that always invokes a JSON parser first may erase the useful distinction between unsupported format and damaged content in a supported format.

Use the actual framework's supported media-type handling and test its configuration. Do not infer the accepted set from the fact that one happy-path request worked.

Use 406 and 415 with their conditions intact

RFC 9110 defines Content-Type, proactive negotiation, and the relevant status codes. A 415 rejection concerns a request's unsupported content format; its cause can involve media type, content coding, or inspection of the content. A 406 concerns unavailable acceptable response representations when the server is unwilling to supply a default.

Accept preferences are not a universal command requiring every server to return 406. The standard permits a server to disregard negotiation preferences in relevant circumstances. If an API promises to honor or reject them, make that an explicit application policy and test it.

Also distinguish media type from content coding. Compressed JSON can have Content-Type: application/json and a separate Content-Encoding. Calling every decompression problem an unknown JSON media type gives the client the wrong recovery question.

Avoid accidental negotiation rules

Media ranges, wildcards, quality values, and parameters need parsing under the HTTP contract. Comparing the raw Accept string to application/json is not a general negotiation algorithm.

Likewise, do not lowercase arbitrary parameter values merely because a media type's type and subtype are case-insensitive. Use a maintained parser or the framework's negotiation facilities, and test the edge cases your interface supports.

Our HTTP caching guide covers representation identity and cache reuse. If negotiation changes the response, revisit cache keys, validators, and relevant Vary behavior as a connected concern; a correct format choice is not automatically safe caching.

Test the consumer's recovery path

A useful integration set includes supported input, unsupported input, malformed supported input, an unacceptable response preference, missing Accept, wildcard acceptance, and relevant parameter combinations. Preserve the actual response body and Content-Type as well as the status.

Check what the client does with an unfamiliar response format. It should not crash while trying to parse an HTML gateway error as the API's normal JSON success object. A documented failure contract needs a usable fallback when an intermediary cannot provide it.

No HTTP server, negotiation library, or conformance suite was executed for this article. The exchange and decision table are synthetic. The desired outcome is an interface in which the client can tell whether to repair its request body, change its response preferences, or investigate a different failure.

You may also like