HTTP Headers
Accept
The HTTP Accept
request and response headers specify which content types, expressed as MIME types, the sender can interpret.
During a request, the server employs content negotiation to select one of the acceptable content types and informs the client of this selection using the Content-Type
response header.
In reply, the server indicates which content types it can understand in messages relevant to the requested resource, enabling the client to utilize this information in future requests to the same resource.
Browsers determine appropriate values for this header based on the context of the request.
For example, different values are used when fetching CSS stylesheets, images, videos, or scripts.
The following table summarizes the types of headers involved:
Header type | Request header, Response header |
---|---|
Forbidden request header | No |
CORS-safelisted request header | Yes* |
* Values cannot contain CORS-unsafe request header bytes, including “( ) : < > ? @ [ ] { } ,” also delete 0x7F
and control characters 0x00
to 0x19
, except for Tab 0x09
.
Syntax
Accept: <media-type>/<MIME_subtype>
Accept: <media-type>/*
Accept: */*
// Multiple types, weighted with the quality value syntax
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8
Directives
<media-type>/<subtype>
A specific and precise media type, such as
text/html
. This indicates exactly what type of data is being referred to.
<media-type>/*
A media type without a specific subtype. For example,
image/*
includes all image formats such asimage/png
,image/svg
, andimage/gif
.
*/*
Represents any media type without restriction.
;q=
(q-factor weighting)
Indicates the relative preference or quality level for a media type through a quality value, known as the weight.
Example
HTTP requests made using command line tools such as curl and wget typically default to using */*
as the Accept header value:
GET / HTTP/1.1
Host: example.com
User-Agent: curl/8.7.1
Accept: */*
Browser navigation usually includes the following Accept header in requests:
GET /en-US/ HTTP/2
Host: developer.mozilla.org
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
…
Once the document is received, the default Accept headers used when requesting images from the developer.mozilla.org website include:
Accept: image/avif,image/webp,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5
Configuring Accept request headers for JSON responses
Systems that interact with APIs often request application/json responses.
Here is an example of a GET request where the client explicitly requests a JSON response:
GET /users/123 HTTP/1.1
Host: example.com
Authorization: Bearer abcd123
Accept: application/json
How to Modify Header using Requestly
Requestly is a powerful Chrome extension that allows you to modify HTTP headers, including the Accept header. This is especially useful for testing how your application responds to different media types during development or debugging. Steps to Modify the Accept Header:
- Install and open the Requestly Chrome extension. You can find it on the Chrome Web Store.
- Create a new rule: Click on “Create Rule” and choose “Modify Headers” from the list of available rule types.
- Add a new header modification:
- Under “Action”, select “Add” or “Override”.
- In the “Header Name” field, enter Accept.
- In the “Header Value” field, enter your preferred media type (e.g., text/html).
- Set the URL condition: Specify the URL or pattern where this header change should apply (e.g., https://your-api.com/*).
- Save the rule.
Once set up, Requestly will inject the Accept: text/html header into all matching requests, allowing you to simulate different client behaviors and test content negotiation on your server.
You might need to modify the Accept header to test how your website or API responds to various content types like JSON, XML, or HTML. This helps ensure your application handles different formats correctly and improves the overall user experience.
Table of Contents
- No headings found.