Client and server can pass the extra bit of information with the request and response using HTTP headers.
The HTTP headers are divided into four categories:
Let's talk about ten widely used HTTP Headers:
Accept
header is used when the client wants to inform the server about the type of data that the client can understand.
For example,
json
Accept: image/png
The Accept-Encoding
header is usually attached with a request to the server, indicating which encoding method is understandable by the client.
For example,
json
Accept-Encoding: gzip
As the header name suggests, the Authorization
request header is used to pass the credentials so that the server can authenticate the client.
For example,
json
Authorization: Basic dgfhWUytzgdfhgSYG
The Accept-Language
request header is used to describe which language is understood by the client.
For example,
json
Accept-Language: en-US
Content-Type
representational header specifies the media type of the resource. This header indicates the content type of the returned data when the client didn't mention any content encoding.
For example,
json
Content-Type: text/html; charset=UTF-8
Content-Location
header indicates the alternate location for the response. It is used when an API can return data in different formats depending on the Accept
header.
For example,
json
Content-Location: /examples/foo.jsonContent-Location: /examples/foo.xml
The Content-Encoding
header is used to compress the message data or payload.
For example,
json
Content-Encoding: gzip
As the header name suggests, it indicates the size of the resource or message body in bytes. It can be used with both request and response.
For example,
json
Content-Length: 148
Content-Language
indicates the human logical language of the response. This header is used to deliver resources in multiple languages so that users can choose their preferred language.
For example,
json
Content-Language: en-US
Cache-Control
header is used as a caching mechanism for both the request and response.
There are several directives(values) that we can pass with this header.
For example,
json
Cache-Control: max-age