The Accept HTTP header tells the server which response formats the client can understand. It is commonly used in REST APIs when a mobile application communicates with a server.
For example, a React Native application can send:
Accept: application/json
This tells the server that the client expects a JSON response.
The Accept header is an HTTP request header that tells the server which media types the client can receive and understand.
Accept: application/json
This is commonly used when working with JSON REST APIs.
A server may support multiple response formats. The Accept header helps the client communicate its preferred response format.
For example:
Accept: application/json
The client is indicating that it can process JSON data.
GET /api/students
Accept: application/json
The mobile application sends a GET request and indicates that it expects a JSON response.
The most common Accept value in the REST APIs used in this course is:
Accept: application/json
It indicates that JSON is an acceptable response format for the client.
| Header | Purpose |
|---|---|
| Accept | Indicates the response format the client can accept. |
| Content-Type | Indicates the format of the message body. |
For example:
Accept: application/json
Content-Type: application/json
GET requests normally do not require a request body, but they can include an Accept header.
GET /api/students
Accept: application/json
The server can return the student data as JSON.
The Accept header can also be used with POST requests.
POST /api/students
Accept: application/json
Content-Type: application/json
{
"name": "Rahul",
"course": "React Native"
}
Here, Content-Type describes the request body while Accept describes the desired response format.
In JavaScript or React Native, the Accept header can be added to the Fetch request.
const response = await fetch(
"https://example.com/api/students",
{
method: "GET",
headers: {
"Accept": "application/json"
}
}
);
Suppose a React Native application sends:
Accept: application/json
The server can return:
{
"success": true,
"students": [
{
"id": 1,
"name": "Rahul"
}
]
}
The response can be processed as JSON by the application.
Axios can send the Accept header through its configuration.
const response = await axios.get(
"https://example.com/api/students",
{
headers: {
"Accept": "application/json"
}
}
);
Postman can be used to manually add an Accept header.
Example:
Accept: application/json
The request can then be sent to the REST API and the response can be checked.
A client can indicate that it accepts plain text using:
Accept: text/plain
The server may return a plain text response if it supports that format.
A client can indicate that it accepts HTML using:
Accept: text/html
This is more common for browser-based web communication than for mobile REST APIs.
A client can also specify XML as an acceptable response format.
Accept: application/xml
This is useful when communicating with an API that supports XML responses.
The Accept header can contain multiple media types.
Accept: application/json, application/xml
This indicates that both JSON and XML are acceptable response formats.
The wildcard value */* can indicate that the client accepts any media type.
Accept: */*
For mobile REST API applications, explicitly requesting JSON is often clearer when JSON is the expected response format.
The basic communication can look like this:
React Native
↓
Accept: application/json
↓
REST API
↓
JSON Response
↓
React Native
The Accept header is part of the client's request.
A PHP REST API can inspect request headers when it needs to know what response formats the client accepts.
$accept =
$_SERVER['HTTP_ACCEPT'] ?? '';
echo $accept;
The exact availability of server variables can depend on the web server configuration.
A PHP API can return JSON using the JSON Content-Type response header.
header(
"Content-Type: application/json"
);
echo json_encode([
"success" => true,
"message" => "Data loaded"
]);
The Accept header from the client can indicate that JSON is expected.
The Accept header does not determine whether an operation succeeds. The API still uses HTTP status codes to communicate the result.
200 OK
400 Bad Request
401 Unauthorized
404 Not Found
500 Internal Server Error
Accept describes a media type, while status codes describe the result of the HTTP operation.
The Accept header can be used together with authentication headers.
GET /api/profile
Authorization: Bearer YOUR_TOKEN
Accept: application/json
The Authorization header handles authentication, while Accept indicates the desired response format.
Suppose a React Native application requests a list of students.
GET /api/students
Accept: application/json
The PHP API can return:
{
"success": true,
"data": [
{
"id": 1,
"name": "Rahul"
},
{
"id": 2,
"name": "Amit"
}
]
}
In a TypeScript React Native application, the Accept header can be included in a typed API request.
const response = await fetch(
"https://example.com/api/students",
{
headers: {
"Accept": "application/json"
}
}
);
const data = await response.json();
The returned data can then be assigned to a TypeScript interface.
A REST API can also return error information in JSON when the client expects JSON.
Accept: application/json
Example error response:
{
"success": false,
"message": "Student not found"
}
The HTTP status might also be:
404 Not Found
The Accept header can participate in a process called content negotiation.
Content negotiation allows a client and server to determine an appropriate representation of a resource.
For example:
Accept: application/json
The client is indicating that JSON is an acceptable representation.
You can test an Accept header using Postman.
Accept: application/json
React Native App
↓
HTTP Request
↓
Accept: application/json
↓
PHP REST API
↓
Process Request
↓
JSON Response
↓
React Native App
The Accept header communicates the response format expected by the client.
const response = await fetch(
"https://example.com/api/students",
{
method: "GET",
headers: {
"Accept": "application/json"
}
}
);
if (!response.ok) {
throw new Error(
"API request failed"
);
}
const data = await response.json();
console.log(data);
The application requests JSON and then converts the response into a JavaScript object using response.json().
The Accept header is an important part of REST API communication. It tells the server which response media types the client can accept.
GET /api/students
Accept: application/json
For React Native applications, using Accept: application/json is a common way to indicate that the application expects JSON responses from the REST API.
Remember the main difference:
Accept
↓
What response format can the client accept?
Content-Type
↓
What format is the message body?
Question: What does the Accept header tell the server?