Accept
Description
The Accept
request header is an HTTP header used by clients (such as web
browsers or APIs) to specify the media types that they can process. This informs
the server about the formats the client prefers in response to a request. The
server then selects an appropriate content type based on the provided
preferences and includes it in the Content-Type
response header.
The Accept
header supports multiple MIME types, allowing clients to specify
their preferred formats in order of priority using quality values (q
parameters). If the server cannot serve any of the specified types, it may
return a 406 Not Acceptable
response.
Syntax
The Accept
header follows this syntax:
Components:
<MIME-type>
:
Specifies the media type (e.g.,text/html
,application/json
).q=<quality>
(optional):
A quality factor between0
and1
, where1
is the highest priority.- Multiple types can be listed, separated by commas.
Examples
Requesting JSON Format
A client requests JSON data from a server:
The server responds with JSON data:
Requesting multiple formats with priorities
A client requests HTML, JSON, and XML, preferring HTML over JSON and JSON over XML:
GET /resource HTTP/1.1
Host: example.com
Accept: text/html;q=1.0, application/json;q=0.8, application/xml;q=0.5
The server, preferring HTML, responds with:
HTTP/1.1 200 OK
Content-Type: text/html
<!DOCTYPE html>
<html>
<head><title>Example</title></head>
<body><p>Hello, world!</p></body>
</html>
Requesting any media type
A client indicates that it can accept any media type:
The server can respond with any available format, such as plain text: