Lesson 40 of 158 – Postman Headers
40%

Postman Headers

HTTP headers provide additional information about an API request or response. In Postman, headers are used to tell the server how data should be interpreted, what response format is expected, and to provide information such as authentication tokens.

Note: Headers are an important part of REST API communication and are commonly used when connecting React Native applications to backend APIs.

1. What is an HTTP Header?

An HTTP header is a key-value pair that provides additional information about an HTTP request or response.

Example:

Content-Type: application/json

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

2. Why Headers Are Used

Headers help the client and server understand how to communicate with each other.

Headers can provide information about:

  • Data format
  • Expected response format
  • Authentication
  • Authorization
  • Caching
  • Client information

3. Headers in Postman

Postman provides a dedicated Headers section where you can add, edit, and remove request headers.

After selecting an HTTP method and entering the URL, open the Headers tab to work with request headers.

4. Header Key and Value

Every header normally contains a key and a value.

Key: Content-Type
Value: application/json

The key identifies the type of information and the value provides that information.

5. Add a Header in Postman

To add a header in Postman:

  1. Open the request.
  2. Click the Headers tab.
  3. Enter the header key.
  4. Enter the header value.
  5. Send the request.

6. Content-Type Header

The Content-Type header tells the server what type of data is contained in the request body.

For JSON:

Content-Type: application/json

7. Content-Type with JSON

When a POST, PUT, or PATCH request sends JSON data, the Content-Type header can be set to:

Content-Type: application/json

This tells the server that the request body contains JSON.

8. Accept Header

The Accept header tells the server which response format the client can accept.

For example:

Accept: application/json

This indicates that the client expects JSON data.

9. Content-Type vs Accept

Header Purpose
Content-Type Describes the format of the request body
Accept Describes the response format the client can accept

10. Example JSON Headers

A JSON API request may contain:

Content-Type: application/json
Accept: application/json

These headers provide information about the request and expected response.

11. Authorization Header

The Authorization header is commonly used when an API requires authentication.

For example:

Authorization: Bearer YOUR_TOKEN

The token can be used by the server to identify and authorize the client.

12. Bearer Token

A bearer token is commonly sent using the Authorization header.

Authorization: Bearer abc123xyz

The exact token format depends on the authentication system used by the API.

13. Add Authorization in Postman

In Postman, an authorization header can be added manually through the Headers section.

Key: Authorization
Value: Bearer YOUR_TOKEN

Postman also provides authorization tools that can generate the appropriate request header.

14. Custom Headers

An API can define its own custom headers when required.

For example:

X-App-Version: 1.0

Custom headers should follow the API's documentation and requirements.

15. Request Headers Example

A request may contain several headers:

Content-Type: application/json
Accept: application/json
Authorization: Bearer YOUR_TOKEN

Each header provides different information to the server.

16. Headers with POST Request

Suppose we want to send a JSON POST request.

POST /api/students.php

Content-Type: application/json
Accept: application/json

The request body can then contain JSON data.

{
    "name": "Rahul",
    "course": "ADCA"
}

17. Headers with GET Request

GET requests can also contain headers.

GET /api/students.php

Accept: application/json

The client can use the Accept header to indicate the desired response format.

18. Headers with PUT Request

A PUT request sending JSON data can contain:

Content-Type: application/json
Accept: application/json

The request body may contain the updated resource.

19. Headers with PATCH Request

A PATCH request can also use JSON and authentication headers.

Content-Type: application/json
Accept: application/json
Authorization: Bearer YOUR_TOKEN

The body can contain only the fields that need to be updated.

20. Headers with DELETE Request

DELETE requests can also use authentication headers when the API requires authorization.

Authorization: Bearer YOUR_TOKEN

The API can verify the token before allowing the delete operation.

21. Automatic Headers in Postman

Postman can automatically add certain headers depending on the request configuration.

For example, selecting JSON in the request body can result in a Content-Type header being added automatically.

Always review the Headers section when debugging an API request.

22. Viewing Request Headers

Before sending a request, open the Headers section in Postman and review the key-value pairs.

For example:

Key Value
Content-Type application/json
Accept application/json

23. Response Headers

The server can also send headers back to the client in the response.

Postman displays response headers in the response section.

Examples can include:

Content-Type: application/json

24. Request Headers vs Response Headers

Request Headers Response Headers
Sent by client Sent by server
Provide information about the request Provide information about the response
Configured in Postman's request Displayed in Postman's response

25. Common Header Mistakes

  • Incorrect header name
  • Incorrect header value
  • Missing Content-Type
  • Wrong Accept value
  • Missing Authorization token
  • Expired authentication token
  • Incorrect custom header

26. Debugging Header Problems

If an API request fails because of headers, check:

  1. Open the Headers section.
  2. Check the header key.
  3. Check the header value.
  4. Check Content-Type.
  5. Check Accept.
  6. Check Authorization if required.
  7. Check whether the token is valid.
  8. Send the request again.

27. Headers and React Native

React Native applications can also send HTTP headers when communicating with REST APIs.

Content-Type: application/json
Accept: application/json
Authorization: Bearer YOUR_TOKEN

These headers can be configured using JavaScript or TypeScript API requests.

28. Complete Postman Header Example

Method:

POST

URL:

http://localhost/api/students.php

Headers:

Content-Type: application/json
Accept: application/json

Body:

{
    "name": "Amit Kumar",
    "course": "React Native"
}

29. Complete Authenticated Header Example

An authenticated API request may contain:

Content-Type: application/json
Accept: application/json
Authorization: Bearer YOUR_TOKEN

The server can use these headers to understand the request and verify authentication when required.

30. Postman Headers Summary

Headers provide additional information during API communication. In Postman, headers can be added through the Headers section and are commonly used for JSON data, response formats, authentication, and other API requirements.

Postman
   ↓
Request Headers
   ↓
REST API
   ↓
Response Headers
   ↓
Client

📌 Key Points

  • HTTP headers provide additional information about requests and responses.
  • Headers use key-value pairs.
  • Content-Type describes the request body format.
  • Accept describes the response format the client can accept.
  • application/json is commonly used for JSON API communication.
  • Authorization can be used to send authentication information.
  • Bearer tokens are commonly sent through the Authorization header.
  • Postman has a dedicated Headers section.
  • APIs can also return response headers.
  • React Native applications can send headers with API requests.

🧠 Quick Quiz

Question: Which HTTP header is commonly used to specify that the request body contains JSON?