HTTP headers are key-value pairs included in HTTP requests and responses that provide metadata about the message.

Why not use body?

Even though you can use body for everything, it is a good idea to use headers for sending data that isn’t directly related with the application logic.

For example, if you want to create a new TODO, you will send the TODO payload in the body

{
   description: "Go to gym"
}

But the Authorization information in the headers

Authorization: harkirat

image.png

βœ… Why Use Headers When We Can Send Data in the Body?

Because headers and body serve different purposes.

Header Body
πŸ“’ Meta info about the request πŸ“¦ Actual data or content
Used by server to understand how to handle the request Used by server to process the actual data (e.g. form, JSON)

🎯 Specific Reasons to Use Headers:

1. Authentication

http
CopyEdit
Authorization: Bearer eyJhbGci...