On this page
Before integrating an API, request the same resource with different Accept headers. Comparing the responses helps you discover which formats the endpoint actually returns, how it handles an unsupported preference, and whether a shared cache has enough information to distinguish representations.
Key Takeaways
Accepttells the server which response media types the client can use; the response’sContent-Typeidentifies the representation returned.- An API may honor, ignore, or reject a requested format. Do not assume every endpoint negotiates content.
Vary: Accepttells caches that the request’sAcceptheader matters when selecting a stored response. A comparison alone cannot prove the cache is configured correctly.- Check status, headers, and body together, and consult the provider’s documentation before depending on a particular error or fallback.
How API content negotiation with Accept works
A client sends Accept as a request header. For example, Accept: application/json expresses a preference for JSON. The server’s response might identify its representation with Content-Type: application/json, possibly with parameters such as a character set.
These headers have different jobs: Accept describes what the client is willing to receive; Content-Type describes what was sent. A response with a successful status can still contain an unexpected media type, so inspect both the status and the response headers before parsing its body.
An Accept header can list alternatives. For example, Accept: application/json, application/xml;q=0.5 gives JSON a higher preference than XML. A wildcard such as */* accepts any media type. These preferences do not guarantee that the server implements negotiation or will return a particular format.
Advertisement
Compare three requests to the same endpoint
The following shell example uses curl against a placeholder URL. Replace it with a GET endpoint you are authorized to call, and consult its documentation for supported media types and authentication. Choose a second media type documented for that endpoint; XML below is only an example. The final media type is deliberately unlikely to be supported, but verify that assumption for your API.
The commands require a Unix-like shell, curl, and mktemp. They save headers and bodies separately in a temporary directory and remove them when the shell exits. They do not automatically follow redirects, so a redirect response can be inspected rather than silently replacing it with a response from another URL.
set -eu
url='https://api.example.com/items/42' # Replace with your authorized endpoint.
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
curl -sS -D "$dir/json.headers" -o "$dir/json.body" \
-w 'JSON request: HTTP %{http_code}\n' \
-H 'Accept: application/json' "$url"
curl -sS -D "$dir/xml.headers" -o "$dir/xml.body" \
-w 'XML request: HTTP %{http_code}\n' \
-H 'Accept: application/xml' "$url"
curl -sS -D "$dir/unsupported.headers" -o "$dir/unsupported.body" \
-w 'Unsupported request: HTTP %{http_code}\n' \
-H 'Accept: application/x-tidyurls-unsupported' "$url"
printf '\n--- JSON headers ---\n'
cat "$dir/json.headers"
printf '\n--- XML headers ---\n'
cat "$dir/xml.headers"
printf '\n--- Unsupported-type headers ---\n'
cat "$dir/unsupported.headers"
# Inspect a body only if it is safe to display and you expect text.
printf '\n--- JSON request body ---\n'
cat "$dir/json.body"
Run this as a script, or run the commands in a dedicated shell so the cleanup trap has a clear lifetime. For an authenticated endpoint, add the provider’s required authentication consistently to all three requests. Keep credentials out of shared scripts and logs. If the resource or error body may contain sensitive information, avoid printing or sharing it.
curl normally completes successfully for HTTP error statuses; the printed status is therefore important. A transport failure, such as a connection error, is different from an HTTP error response and stops this script. The example has not been executed here.
Interpret the comparison
For each request, record the HTTP status, Content-Type, Vary, relevant cache directives, and enough of the body to understand the result. If your connection passes through a proxy or receives an interim response, the saved header file can contain multiple header blocks; identify the block for the response you are evaluating.
- Different content types and bodies: The endpoint may be selecting different representations. Confirm that each body matches its declared type and the provider’s documented schema.
- The same response for all three: The endpoint might ignore
Accept, always return one format, or apply a fallback. This comparison alone does not establish which explanation is correct. - An error for the unsupported type: Inspect its status,
Content-Type, and body. A server can respond with406 Not Acceptablewhen it cannot provide an acceptable representation, but do not assume every API will do so. Errors can themselves have a different content type from successful responses. - A redirect: Inspect the
Locationheader and the provider’s documentation before making a separate request to the destination. Ensure it is an authorized, expected host; do not forward credentials indiscriminately.
RFC 9110 and the provider’s official documentation are important for interpreting these results and deciding what behavior to rely on. Neither was available to consult for this article, so the example does not claim a provider-specific supported format, fallback, or error response. Check the documentation for your endpoint and API version before implementing the integration.
Advertisement
Understand Vary and shared caches
A shared cache can serve stored responses to multiple clients. When an endpoint selects representations based on Accept, a response containing Vary: Accept signals that a cache must take that request header into account when matching a stored response to a later request. Otherwise, a cache that reuses the wrong representation could deliver, for example, a JSON response to a client expecting another format.
Vary does not by itself make a response cacheable or prove that a CDN, proxy, or application cache honors it. Check the response’s cache directives and your actual cache configuration, particularly for authenticated or user-specific responses. Vary can also name other request headers; Vary: * has different reuse implications from a named list.
Comparing direct responses to one URL cannot reveal every cache key, intermediary, or cache policy in the deployment path. It also cannot prove that all variants are isolated or that personalized data is safe in a shared cache. Test through the real cache path in an authorized environment, with controlled requests and cache diagnostics provided by your infrastructure, and review the provider’s caching guidance.
Common Mistakes
- Checking only the body. A proxy error page or authentication error can look like a valid response unless you also inspect the status and
Content-Type. - Assuming
Acceptforces a format. The server may ignore the preference or use a documented fallback. - Changing more than
Accept. Keep the URL, method, authentication, and other relevant headers constant so differences are easier to interpret. - Treating
Content-Typeas proof of valid data. Validate the body and handle parsing errors even when the header looks correct. - Assuming an absent
Varyproves safe caching. It may be irrelevant for a non-negotiated response, or it may expose a configuration problem; inspect the actual behavior and cache policy.
Best Practices
Request only formats your client can process, and compare the returned media type without assuming parameters will be absent. Handle non-success statuses and unexpected content types before deserializing. Document the provider’s supported formats, fallback behavior, and API version in your integration, and repeat these checks when the provider changes its contract.
Conclusion
A controlled set of requests to the same endpoint can reveal useful differences in status, format, and cache-related headers. Use the results alongside the provider’s documentation and real cache-path testing—not as a substitute for either.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.