Headers are like metadata — they contain important information about the request you’re sending over, but they are not the meat of the request.
You need to know two things here:
- How to set headers
- How to check the headers you have sent
Setting headers
The two most common request headers are:
Content-Type: This tells the server what type of data you’re sending overAuthorization: This lets you authenticate with the server.
You can set headers by passing a key:value pair into the request’s options. Here’s an example of how you’ll do this with the Fetch API.
// fetchfetch('https://link-to-resource', { headers: { 'Content-Type': 'application/json', Authorization: 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', },})We’ll talk more about the Content-Type in the next lesson and Authorization in a later lesson.
Viewing request headers
You can view the request headers from the Network Tab.
Here are the steps:
- Click on the Network Tab
- Click on the request you sent.
- Look at the Headers Tab
When you’ve done this, you should be able to see both Request and Response Headers.
Here’s an example of the request headers you may find.
There are a lot of headers here.
You don’t need most of them — you only need the two headers I introduced (Content-Type and Authorization).
For the rest, you can always Google them when you need to find out more about them. (And you probably won’t have to do this at all).
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson