Lesson 28 of 158 – Accept Header
28%

Accept Header

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.

Note: The Accept header describes the response format the client wants. The Content-Type header describes the format of the request or response body.

1. What is the Accept Header?

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.

2. Why is Accept Important?

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.

3. Accept Header Example


GET /api/students

Accept: application/json

The mobile application sends a GET request and indicates that it expects a JSON response.

4. Accept: application/json

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.

5. Accept vs Content-Type

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

6. Accept Header with GET

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.

7. Accept Header with POST

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.

8. Accept Header with Fetch

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"
        }
    }
);

9. Accept and JSON Response

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.

10. Accept Header with Axios

Axios can send the Accept header through its configuration.


const response = await axios.get(
    "https://example.com/api/students",
    {
        headers: {
            "Accept": "application/json"
        }
    }
);

11. Accept Header in Postman

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.

12. Accept: text/plain

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.

13. Accept: text/html

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.

14. Accept: application/xml

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.

15. Accept Multiple Formats

The Accept header can contain multiple media types.


Accept: application/json, application/xml

This indicates that both JSON and XML are acceptable response formats.

16. Accept Wildcard

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.

17. Accept Header and API Response

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.

18. Accept Header in PHP

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.

19. Returning JSON from PHP

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.

20. Accept Header and Status Codes

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.

21. Accept Header with Authentication

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.

22. Accept Header with Student API

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"
        }
    ]
}

23. Accept Header with React Native TypeScript

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.

24. Accept Header and Error Responses

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

25. Common Accept Header Mistakes

  • Confusing Accept with Content-Type.
  • Assuming Accept controls the request body format.
  • Using an unsupported response media type.
  • Forgetting that the server may support only certain formats.
  • Using a wildcard when a specific response format is required.
  • Not handling an unexpected response format in the application.

26. Accept Header and Content Negotiation

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.

27. Testing Accept Header in Postman

You can test an Accept header using Postman.

  1. Open Postman.
  2. Create a GET request.
  3. Enter the API URL.
  4. Open the Headers tab.
  5. Add the Accept header.
  6. Set its value to application/json.
  7. Send the request.
  8. Inspect the response.

Accept: application/json

28. Complete Accept Header Flow


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.

29. Complete React Native Example


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().

30. Complete Accept Header Concept

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?

📌 Key Points

  • Accept is an HTTP request header.
  • It tells the server which response formats the client can accept.
  • Accept: application/json is commonly used with JSON APIs.
  • Accept is different from Content-Type.
  • Content-Type describes the message body format.
  • Accept can be used with GET, POST, PUT, PATCH and DELETE requests.
  • Fetch can send the Accept header through the headers object.
  • Axios can also send the Accept header.
  • Postman can be used to test Accept headers.
  • PHP can inspect the Accept header.
  • Accept can contain multiple media types.
  • */* represents any media type.
  • Accept can participate in content negotiation.
  • Accept does not determine HTTP success or failure.
  • React Native applications commonly use JSON responses.

🧠 Quick Quiz

Question: What does the Accept header tell the server?