How to Define Content-Type for a JSON API and Verify Rejection Before Data Changes
Defining Content-Type for a JSON API means specifying the permitted body format for a particular operation and ensuring that requests that fail this requirement are rejected before data changes. But a header, or even the expected HTTP status code, is not enough to verify the outcome. We’ll connect the contract, JSON parsing, and observable record state using an entirely fictional service and synthetic data we control.
What to specify in the contract
Start with one operation rather than a general requirement that ‘the API uses JSON.’ Document the supported content type and rejection behavior. A request with a body requires a Content-Type supported by the operation’s contract; an unsupported media type should be rejected with 415 Unsupported Media Type.
In our fictional service, ‘Draft,’ the record-renaming operation accepts a body only with Content-Type: application/json. Its contract also explicitly specifies a 415 response for a request that has a body but lacks this header. This is a rule chosen for the example: it needs to be documented and tested separately. The absence of a body alone is not a reason to require Content-Type. Status code 406 does not apply here: it concerns negotiation of the response format.
Where the trust boundary lies
The content type declared by the sender does not establish that the body actually uses that format. The boundary before the business action therefore includes two sequential conditions: an allowed Content-Type and successful parsing of the body by a safe parser. Only after both conditions are met should the example operation proceed to rename the record.
If an unsupported format is rejected only after saving, the required behavior has not been achieved, even if the client receives 415. Likewise, an allowed application/json header is no reason to skip parsing the body. The body must match its declared content type; a formatting error makes it a bad request, for which status code 400 is provided.
Prepare a fictional record and a positive control
The following checks are an author-developed defensive test plan, not an official OWASP procedure. Run them only in your own test environment with synthetic data. Create record demo-17 with the name ‘First draft.’ Prepare the valid JSON body {"name":"New draft"} for the operation. Provide a way to inspect the saved record and a counter for calls to the rename business action in the test environment.
Before each case, restore the original name and reset the counter. First, send the prepared body with Content-Type: application/json. The positive control passes when the operation returns its normal success response, the saved name becomes ‘New draft,’ and the counter shows one call to the action. A successful HTTP response without checking the record does not confirm that the rename occurred.
Test rejection of unsupported types
Send the same valid JSON body again, separately using text/plain, application/x-www-form-urlencoded, and multipart/form-data. Our example’s contract excludes these types. They are content types used by simple browser requests; OWASP discusses disallowing them as one defensive measure. This check tests a specific rule for the JSON operation without modeling actions against someone else’s application.
Each case passes only when all three observations match: a 415 response, the saved name ‘First draft,’ and a business-action counter of zero. If the response is correct but the name changed or the action was called, rejection did not occur at the required boundary. That result requires correcting the request-processing order.
Test a missing header and malformed JSON separately
To test a missing Content-Type, send the valid JSON body without the header. Verify that the test client did not add it automatically; otherwise, you are testing a different case. Under the fictional operation’s contract, expect 415, the original name, and a counter of zero. Do not automatically apply this criterion to requests without a body.
Then restore the initial state and send an unclosed JSON object with Content-Type: application/json. The example plan expects 400, an unchanged record, and no call to the business action. This case demonstrates that an allowed header does not replace body parsing. Field-value validity is not tested here: the scope is limited to message format and rejection before the action.
Which fixes to prioritize
First, eliminate the possibility of calling the business action before checking the content type and successfully parsing the JSON. Then align the implementation with the contract: list the supported types, specify rejection of unsupported formats, and document the chosen behavior when a request with a body lacks the header. After fixing the issues, repeat the positive control and every negative case, restoring the record each time.
Save the results as a set of observations: the header sent, HTTP status code, final name, and number of calls to the action. This makes discrepancies between the API response and the actual state visible. Passing these cases confirms the operation’s tested behavior in the test environment, but provides no statistical guarantee of product security.
Test limitations and a web-product audit
Checking Content-Type addresses the narrow task of allowing a format before a business action. It does not replace field-value validation, object authorization, or CSRF protection. Web applications using cookie-based authentication still require CSRF protection. HTTP method configuration, GraphQL mutations, file uploads, CSRF tokens, SameSite, CORS, and JWT validation are outside this plan’s scope.
The practical outcome is a clear contract for one operation and reproducible checks for an accepted change and rejections that leave the record unchanged. To assess your website or the web portion of your product, request a free CheckMyApp audit, specifying the selected JSON operation and the behavior expected under its contract.