How to use headers in REST APIs? Different types of headers, how and where to use?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to pseudocode we have been talking a lot about rest apis in past 45 videos and also we have talked about headers but we have not explored headers in depth you might have been afraid of headers and you might get uncomfortable feeling when you see headers like x hyphen something something and it might seem scary or something cryptic to you but trust me it is very simple and i'm going to use a very easy analogy for you to understand what http headers and rest pay headers are in this video we are going to cover the same i am going to cover the concept of headers with some examples and also i'll share some links so that you guys can understand headers in detail so let's get started [Music] you already know that apis are a way to transfer data from one node to another or from one system or one machine to another okay now when you transfer that data you have some protocols and we have seen that how you can modify resources create resources and fetch resources okay now when you transfer this data just imagine that you are transferring some piece of information or some knowledge closed in an envelope from one person to another okay like say one person is client another person is server and an envelope is getting transferred in form of api and that is the mode of communication the contents inside that envelope are actually the data that is getting transferred from one person to another but you might also know that when such communication happens in real life when letters get exchanged or important papers get exchanged on the top of the envelope there is also the address to whom this data has to go to right and there are also addresses that if this data cannot go to the consent person then please return this envelope to the concerned person who has sent it right this is just an analogy and i just want to explain that headers also play a similar kind of rule headers are sort of indication for the metadata of what the response or request consists inside now in order to understand this in detail let me start with categorizing the headers so api headers can be categorized or can be grouped into four different categories there are request headers response headers payload headers and representation hedges now this does not mean that a particular header which is a request header cannot be a response header or both of them cannot be a payload header right it is just based on different context you can understand that there can be different types of headers let's get started with the request header first request headers are actually the key value pairs or any header is actually a key value pair that are sent by the client who is requesting for the data so that server can understand how the server has to send the response and also the server can get some knowledge about the requesting uh body or the client okay that is called a request header some example of request headers are host user agent referral connection accept language and so on now please note that content type is not a request header content type is categorized as one of the representation headers which we are getting to very soon now from this list you can easily see the key and value pairs which are headers and understand that what these mean so the user agent or the host are holding the information that who is sending this request except language and accept encoding are actually the headers which are telling server that this is the language i'm going to understand the response in so let's say if the accept language is en which means english that means that the client is telling the server that i'm going to understand the response in english okay similar same thing goes for accept encoding if the client says that i can accept encoding gzip that means it says that if you are going to send me compressed data i can accept that kind of response so these are all the request headers which the client is sending so that the server can understand how it has to form its response okay so this is the role of request headers there are many more values and examples of request headers which are shared in the description next let's talk about response headers you might think that response headers are just like request headers where server is telling the client who is requesting for some information that what to do with response it is more or less true response headers are more like that server is giving some additional information about the data that it has sent okay let's see how in the example of response header you can see values like connection date server content type transfer encoding etc okay all these values have different meanings and usually these values are giving some information about the response for example that date is giving the response that this is the date that i have set date and time stem that i have sent the response last modified is telling that this is the time that response have been updated or modified e-tag is one of the important response headers that you should understand it is used for versioning and cache if you want to know more about it let me know in comments and i will cover it in a separate video once again i would like to mention that content type and content encoding are usually considered in the category of representation headers but uh technically speaking those are also part of response headers or request headers i just wanted to note this distinction that content type content length etc are representation headers which we are going to discuss now now let's talk about representation header as you might have guessed from the name of representation itself representation headers talk about the data that has been transferred okay now the data that has been transferred from one server to another can be in different formats it can be xml it can be json it could be encoded in different formats it can be sent as one piece of data at a time or the response is so huge that it has to be chunked and sent in different chunks or different set of bytes and then the server has to tell the client that okay i am going to send different chunks of data for one response and this is the content range that i am going to use so that you can read it right this is the importance of representation headers and the examples of representation headers are content type content encoding content length content range and content location content location is one of the fields that we might be discussing in future also it just says or tells about an alternate location of the resource or the data that is available for the client to retrieve the information from so it can be a url where that particular resource is sitting apart from these three categories there can be different headers like trailer or transfer encoding or etag if none then match authorization headers and many more if you want to know about any of these in detail you can let me know in comments and i will try to create a separate video on the same now these are the predefined http headers right now what happens if you want to if you are writing apis and you are implementing apis and you want to define your own custom headers can you do that yes you can do that the way you are defining the request structure of your apis and the response structure of your apis similarly you can define and implement different headers that you are going to accept for example many people use authorization headers in order to uh authenticate the user who are going to use their apis so authorization is one of the famous uh headers that people use and customize it accordingly and there are many more uh headers that you can use custom headers basically that you can use in order to implement the logic on your site where you don't want that information to come to the server in the form of payload you want uh it is not directly related to the payload or the data it is some kind of metadata or you want to do some extra processing on it it is needed for the context of the application that kind of information goes into custom headers and you can implement your own headers as well i hope with these examples and these different categorizations it might have become a little bit clear to you what headers are used for what are the different kind of headers and how you can implement custom headers if there are any uh headers or any particular headers which you don't understand or which you want to refer to i have included many resources in the description in spite of that if you feel uh any of it is confusing or if you want to dig deep into one of the headers or men or a couple of headers you can always uh comment on the video and i will try to make a follow-up video till then take care see you in the next video [Music] you
Info
Channel: sudoCODE
Views: 45,190
Rating: undefined out of 5
Keywords: REST, REST API, HTTP headers, GET, put, post, delete, patch, api, application programming interface, Representational State Transfer, Uniform Resource Interface, Response Data, Request Payload, what is rest api, rest api introduction, rest api course, rest api explained, rest api tutorial, how to study rest apis, what are rest apis, sudocode, yogita sharma, resource, rest api basics, rest api explain, headers in rest api, HTTP Request Response Headers Tutorial, HTTP Crash Course
Id: 76CcJ90Lz4U
Channel Id: undefined
Length: 8min 36sec (516 seconds)
Published: Sat Nov 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.