Lesson 18 of 158 – HTTP Response Headers
18%

HTTP Response Headers

HTTP response headers are pieces of information sent by the server to the client along with an HTTP response.

When a React Native application communicates with a REST API, the server can use response headers to provide information about the response, such as its content type, caching instructions, server information, and other response metadata.

Note: Request headers are sent from the client to the server, while response headers are sent from the server back to the client.

1. What is a Response Header?

A response header is a key-value pair sent by a server with an HTTP response.

Example:


Content-Type: application/json

The server uses this header to tell the client what type of content is included in the response.

2. Why Response Headers are Used

Response headers provide additional information about the server's response.

They can be used to:

  • Describe the response content.
  • Control caching.
  • Provide security-related information.
  • Provide information about the response.
  • Control certain client behaviors.

3. Response Headers and REST API

REST APIs commonly use response headers to provide metadata along with JSON responses.


React Native
      ↑
HTTP Response
      ↑
Response Headers
      ↑
PHP REST API

The mobile application receives both the response headers and the response body.

4. Response Header Format

A response header contains a name and a value separated by a colon.


Content-Type: application/json

The header name is Content-Type and the value is application/json.

5. Common Response Headers

Some commonly encountered response headers include:

  • Content-Type
  • Content-Length
  • Cache-Control
  • ETag
  • Location
  • Set-Cookie
  • Access-Control-Allow-Origin

The exact headers returned depend on the server, framework, API, and request.

6. Content-Type Response Header

The Content-Type response header tells the client what type of content is returned by the server.


Content-Type: application/json

REST APIs commonly use application/json when returning JSON data.

7. JSON Response Header

A PHP REST API can specify that its response contains JSON.


header("Content-Type: application/json");

After setting the header, the API can return JSON data.


echo json_encode([
    "success" => true,
    "message" => "Data loaded"
]);

8. Response Headers with GET

A GET request can receive response headers together with the requested data.


GET /api/students

The server might return:


HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": []
}

9. Response Headers with POST

After a POST request creates a resource, the server can return response headers describing the result.


HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/students/25

The response can also contain a JSON representation of the newly created resource.

10. Response Headers with PUT

A PUT request can also return response headers after successfully updating a resource.


HTTP/1.1 200 OK
Content-Type: application/json

The server can return the updated resource in the response body.

11. Response Headers with PATCH

A PATCH request can return response headers together with the result of a partial update.


HTTP/1.1 200 OK
Content-Type: application/json

The response can contain the updated information or a success message.

12. Response Headers with DELETE

A DELETE request can also return response headers.


HTTP/1.1 204 No Content

When there is no response body, the API may use 204 No Content.

13. Reading Response Headers in Fetch

The Fetch API provides access to response headers through the response object's headers property.


const response = await fetch(
    "https://example.com/api/students"
);

const contentType =
    response.headers.get("Content-Type");

console.log(contentType);

14. headers.get()

The get() method can be used to retrieve a specific response header.


const value =
    response.headers.get("Content-Type");

console.log(value);

If the requested header is not available, the result can be null.

15. Checking Response Content-Type

A mobile application can inspect the response Content-Type when needed.


const contentType =
    response.headers.get("Content-Type");

if (contentType &&
    contentType.includes("application/json")) {

    console.log("JSON response");

}

This can help the application understand the type of response data.

16. Content-Length Header

The Content-Length header can indicate the size of the response body in bytes when provided.


Content-Length: 250

The actual presence and behavior of this header can depend on the server and how the response is transmitted.

17. Cache-Control Header

The Cache-Control response header provides caching instructions to clients and intermediate caches.


Cache-Control: no-cache

Caching behavior depends on the directives provided by the server.

18. ETag Header

An ETag can identify a particular version of a resource.


ETag: "abc123"

ETags can be used with conditional requests to help determine whether a resource has changed.

19. Location Header

The Location response header can provide the URI of a resource or another relevant location.

For example, after creating a student:


HTTP/1.1 201 Created
Location: /api/students/25

The URL identifies the newly created resource.

20. Response Headers in PHP REST API

PHP can send response headers using the header() function.


header("Content-Type: application/json");

echo json_encode([
    "success" => true,
    "data" => []
]);

The header should be sent before the response body is output.

21. Setting Multiple Response Headers

PHP can send multiple response headers.


header("Content-Type: application/json");
header("Cache-Control: no-cache");

Each header provides different information or instructions to the client.

22. Response Headers and HTTP Status

Response headers are sent together with the HTTP status and response body.


HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": []
}

The status code describes the result, while the headers provide additional response information.

23. Response Headers with Axios

Axios provides access to response headers through the response object.


const response = await axios.get(
    "https://example.com/api/students"
);

console.log(response.headers);

A specific header can be read from the headers object.


console.log(
    response.headers["content-type"]
);

24. Response Headers and Postman

Postman displays response headers after an API request is sent.

For example:

Header Example Value
Content-Type application/json
Cache-Control no-cache
ETag "abc123"

This makes Postman useful for inspecting how a REST API responds.

25. Response Headers and CORS

CORS-related response headers can control whether browser-based clients from another origin are allowed to access a response.

For example:


Access-Control-Allow-Origin: *

CORS is primarily a browser security mechanism. React Native applications do not use browser CORS in the same way as web browsers.

26. Response Headers and Authentication

Authentication systems can also use response headers in certain API designs.

For example, a server can provide information about authentication or related response metadata through headers.


HTTP/1.1 401 Unauthorized
Content-Type: application/json

The actual authentication flow depends on the API design.

27. Response Headers Flow


React Native
      ↓
HTTP Request
      ↓
PHP REST API
      ↓
Process Request
      ↓
HTTP Status
      +
Response Headers
      +
JSON Body
      ↓
React Native
      ↓
Read Response

The client receives the complete HTTP response and can use the information it needs.

28. Common Response Header Mistakes

  • Forgetting the Content-Type header for JSON responses.
  • Sending headers after output has already started in PHP.
  • Assuming every API returns the same headers.
  • Ignoring important caching instructions.
  • Trying to read a header that the server did not provide.
  • Confusing response headers with request headers.
  • Assuming CORS works the same way in React Native and browsers.

29. Complete Response Header Example


HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-cache

{
    "success": true,
    "data": [
        {
            "id": 1,
            "name": "Rahul"
        }
    ]
}

The status code describes the result, the response headers describe additional information, and the JSON body contains the actual API data.

30. Complete HTTP Response Headers Concept

HTTP response headers provide metadata and instructions about the response returned by a REST API.


PHP REST API
      ↓
HTTP Status Code
      ↓
Response Headers
      ├── Content-Type
      ├── Cache-Control
      ├── ETag
      └── Location
      ↓
JSON Response Body
      ↓
React Native

Understanding response headers is important before working with advanced REST APIs, caching, authentication, CORS, and mobile API integration.

📌 Key Points

  • Response headers are sent from the server to the client.
  • Response headers contain metadata about an HTTP response.
  • Content-Type describes the response content format.
  • REST APIs commonly return application/json.
  • Cache-Control provides caching instructions.
  • ETag can identify a particular version of a resource.
  • Location can identify a resource location.
  • PHP can send response headers using header().
  • Fetch can read response headers using response.headers.
  • Axios provides access to response headers through response.headers.
  • Postman can be used to inspect response headers.
  • CORS-related headers are important for browser-based API clients.
  • Response headers are different from request headers.
  • Response headers are sent together with the HTTP status and body.

🧠 Quick Quiz

Question: Which response header commonly tells the client that the response contains JSON data?