← All articlesWeb security

How to Define a JSON API Response Type and Reject Unsupported Accept Values

A client requests JSON, but a successful status alone is not enough: you need to check the actual response body and Content-Type. The reverse case also needs a clear rule: what happens when the client accepts only XML but the operation provides JSON? We will examine the contract for a single read operation using an entirely fictional catalog with our own synthetic records. The result is a set of observable criteria that developers and product owners can use to check response format negotiation.

Define the operation contract first

For this educational example, we will use the /catalog/items read operation. All data belongs to our fictional project: the catalog contains two synthetic records with the identifiers demo-101 and demo-102, named ‘Demo Lamp’ and ‘Demo Shelf’. The only supported representation is application/json. No default representation is provided when Accept is incompatible. This is an explicit condition of the example, and the expected HTTP 406 depends on it.

Document these rules for the operation. Clients should understand which format they can request and what they will receive when no acceptable representation is available. Saying ‘the API returns JSON’ is insufficient if it does not define behavior when the client's preferences are incompatible.

The trust boundary between Accept and Content-Type

Accept communicates the client's preferences, while Content-Type describes the server's actual response body. A value supplied by the client must not simply be copied into the response header. The server selects a type from its own set of supported representations, taking Accept into account. Our contract supports just one type: application/json.

Establishing JSON as the response format means keeping the body and header consistent: the server returns actual JSON and labels it Content-Type: application/json. Replacing this value with application/javascript does not match the selected representation. Checking the header alone is also insufficient: the body must match the declared type.

Consider the entire Accept value, including ranges and quality values

Negotiation cannot be reduced to checking whether the entire Accept string exactly matches application/json. Preferences can contain multiple types, media ranges, and quality values. For example, Accept: application/xml, application/json;q=0.5 still makes JSON acceptable: in our catalog, the server selects the supported application/json representation, even though XML has a higher preference.

The range Accept: application/* also permits JSON. By contrast, Accept: application/json;q=0 explicitly excludes the only representation in our example. If no acceptable option exists and no default representation is provided, HTTP 406 Not Acceptable is expected. Preserve this condition in both the implementation and the checks.

Run a positive control using your own data

The following checks are an author-designed defensive plan for our own fictional catalog, not an official testing procedure. First, prepare the two synthetic records described above and perform the read operation with Accept: application/json. Save the status, Content-Type, and actual body from the same response so that related results can be compared.

The acceptance criterion consists of several observations: the response is successful, Content-Type specifies application/json, and the body parses as JSON and contains both expected records with the agreed identifiers and names. A successful HTTP status alone does not confirm that the contract has been met. Since this example concerns reading, there is no need to confirm a state change: the check concerns the catalog representation received.

Check rejection and selection of a supported representation

Using the same set of your own data, send separate requests with Accept: application/xml and Accept: text/html. For both, the acceptance criterion is HTTP 406 under our contract, which provides no default representation when Accept is incompatible. If the operation successfully returns the catalog instead, the chosen rejection rule has not been met.

Next, send Accept: application/xml, application/json;q=0.5. Expect a successful response, actual JSON containing the two demo records, and Content-Type: application/json. The response header must not contain a copied preference string. Finish with two cases: application/* should produce JSON, while application/json;q=0 should produce HTTP 406. For the successful case, check the body, header, and expected records together again.

Prioritize the fixes

First, align the documentation and implementation: the operation's sole representation, the condition that no default is available, and the expected rejection must match. Then fix type selection: the server must account for supported representations, ranges, and quality values, and set Content-Type according to the selected representation. Remove any direct copying of Accept.

The next priority is ensuring that the body matches the header. After making fixes, repeat the positive control and rejection cases using the same synthetic catalog. Save the expected and actual results for each request: this will show exactly which contract conditions have been met and which need further work.

Limitations of the checks and the next step

The results apply to one operation and the selected set of Accept preferences. They do not prove that the entire API is secure or provide statistical guarantees. The HTTP 406 rule in this example cannot automatically be applied to APIs that provide a default representation: that is a separate condition of the contract.

The practical outcome is a verifiable relationship between the client's preferences, the type selected by the server, and the actual JSON. If you need to check this contract in your product, you can start with a free audit of your website or the web portion of your product and identify operations whose response negotiation behavior needs to be defined.

Sources and documentation

  1. REST Security Cheat Sheet
  2. Input Validation Cheat Sheet
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free