#09 Request & Response Headers | HTTP Request & Response | ASP.NET Core MVC Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the last lecture we had a brief look at HTTP request and response headers now in this lecture let's talk about HTTP request and response headers in detail HTTP request headers are the key value pairs that are sent by the client to the server and it contains information about the request which can be used by the server for processing the request keep in mind that the request headers are prepared by the client another example since we are making a request using the browser the browser is the client and the browser is the one which will prepare the request headers let's go to the browser and here I am using the Chrome browser from here let's go ahead and let's open the developer tools and here let's go to this network tab okay so when I reload the page a request will be made to our asp.net core application basically to this URL and it is this URL on which our asp.net core application is hosted so when I make this request you will see that a request has been logged here if I click on this request here you can see the request headers so all these request headers are prepared by the Chrome browser okay so here the Chrome browser is the one which is preparing the request and it is going to send this request to the server because here the Chrome browser is the client so as I mentioned earlier the client is the one which prepares the request headers and it sends it to the server if you are using an Android app in that case the client is that Android app so that Android app will prepare the request headers before sending it to the server in the same way if you are using a tool like Postman for testing your apis or controllers in that case that Postman tool is the client so when we send a request using the postman tool that Postman tool is going to prepare the request headers so in simple words we can say that using the request headers a client like browser or a tool like Postman or an Android app or an IOS app these applications these clients talk to the server using the request headers for example here we have this request header called accept language and it is set to en US so basically here the client the browser is telling server that when the server will send the response it needs to send the response in the English language in the same way sometimes as the part of the request when we are sending a post request put request of patch request we might need to send a body with the request so it is the responsibility of the browser or the client to tell the server what type of information it is sending with the request okay now let's see some of the important request headers which we might need while developing an application first of all we have this accept header and this accept header represents what type of response the client can accept from the server for example when we are using a browser in case of browser browser is the client and it can accept a response of type text Json HTML Etc but if the client is an Android app in that case it cannot accept a response of type HTML because HTML content can be rendered by the browser but it cannot be rendered by an Android app or IOS app so using this accept request header we tell the server what type of response the client can accept then we have this accept language so here we are basically telling in which language the client is expecting the response then we have the content type the content type header tells the server what type of request body it is sending whether it is sending the text data or Json data or HTML data or what kind of data the client is sending to the server that is represented by this content type request header then we have this content length request header and it tells the length of the request body in bytes then we have this date header this date header tells when the request was made and we also have this user agent header so this header basically tells which browser has been used in order to make the request now the next question is how can we access these request headers from our asp.net core application here I am in the asp.net core application now let me remove these two lines of code from here we don't need it and let's say from here we want to access the request header called user agent and we want to know which browser has made the request so first of all all the request and response information we are going to get on this HTTP context object so here let's copy this context object from here and on that we have this request object and on that request object we have a property called headers okay now keep in mind this headers is a dictionary and it contains all the request headers as a key value pair so here let's first check if the request headers contains a request header called user agent okay so here let's use an if statement and let's wrap this statement inside that if condition so here we want to check if the request header contains this request header so here on this headers we are going to use a method called contains key and let's pass this user agent to that and here we don't need these square brackets okay so first we are checking whether this request header is available on this headers dictionary or not if it is available then we want to know its value so again on the context object let's first access the request object on that request object let's access the headers and since it is a dictionary the headers is a dictionary we can use its key to access its value so for that we can use square brackets like this and inside that we can specify the key for which we want to access the value here we want to access the value for this user agent so let me copy this and let me specify it here okay and let's go ahead and let's assign it to a variable for that I'll create a variable and I will call it user agent okay so using this contains key we can check whether a request header is available in this header's dictionary or not and in order to access the value of a request header we can use it like a dictionary because we learned that this headers is a dictionary so to this dictionary when we specify the square brackets and inside that square brackets we can specify the key so based on that key it is going to return us the value for that key and keep in mind that all the keys in the request header are going to be a string so let's go ahead and let's display this user agent in the browser so let me paste it here and here let's say user agent and let's go ahead and let's define this user agent out of this if statement so that it will be available outside this if statement okay and since we are declaring this variable using this work keyword we need to assign some value to it so here I'm going to assign an empty string to it with this let's go ahead and let's run this application and now in the browser it should lock the user agent from the request header and here you can see the user agent value has been logged here if I open the developer tool there if we go to this network tab and let me make a request One More Time by reloading the page and here if we see the user agent if I scroll down somewhere we should have this user agent so here we have this user agent request header so this is the value for that user agent and the same value has been displayed here in the browser okay so in this way you can access any of these request headers okay these request headers are created as the elements of this headers dictionary in this dictionary this header name will be the key and its value will be the value okay now let's talk about response headers so let's go back to our slides HTTP response headers are the key value pairs just like HTTP request headers and it is sent by the server to the client in the response HTTP response headers includes information about the response itself which can be used by the client so that the client can manage the response accordingly generally these response headers are not visible to the end user it is only intended for the information exchange between the server and the client let's see some of the common response headers that you might want to use or know about as a backend developer while creating the response because as I have mentioned earlier it is US back-end developers who creates the response so it is US back-end developers who set the response headers on the response so these are some of the common response headers which you might want to use the first one is the server and this response header tells which server is sending the response then we have this cache control so this cache control response header indicates the number of seconds the response can be cached in the browser's memory so if we set it to 60 seconds that means the response which has been sent from the server that will be cached in the browser's memory for 60 seconds so within that 60 seconds if the browser makes a request again in that case instead of sending the request to the server the browser will fetch the response from the browser's cache memory and we can also set this cache control to no cache that means the response will not be cached in the browser's cache memory then we have this content type so this content type tells the client what type of response is sent by the server whether the response is an HTML response or it is a Json response or it is a simple text response so that information is conveyed by this content type response header then we have this content length response header this content length response header tells what is the length in bytes of the response body then we have this date header it tells at what date and time the response was sent to the client and finally we have this set cookies and it contains cookies to be sent to the browser basically when we are sending a response to the client with the response we can also send some cookies for example here we are sending a cookie called x with its value as 20. and we also have other type of response headers but these six are the important one and these six are the one which we are going to work with a lot in this course apart from these six there is also another response header called location the location contains the URL to redirect it can be used for redirection from one URL to another URL and we will practically use this location response header when redirecting from one action method to another action method when we will work with MVC architecture now let's go ahead and let's set some response headers in practice let me go ahead and let me remove this if statement and this user agent okay and from this method let's simply send a text response okay so from here let's say this is a text response okay this is just for demo if I save the changes and if you run this application we should see this text in the response right if I go ahead and if I run this application you can see we have this text in the response now if I open the request so let me go to this network tab let's make this request again and let's open this request and here let's go to this response headers and here if you see the content type of this response header is by default text last plane okay that means here we are telling the browser that the server has sent a response and that response is of type plain text so that's why if I go ahead and if I make this text and HTML so for example let's say instead of a text we want to send some HTML response so let's say I want to send an H2 element in the response if I do the hot reload here and if we refresh the page you see we are seeing that HTML as a text it is not rendered as an HTML in the browser because here the server has told the client that it is sending a text response here we are telling that we are sending a text response so the client is treating that response as a text but we can go ahead and we can set the content type as text HTML in that case the client will treat this text as a HTML response and to do that let's go to visual studio let me stop the application here and on this context object we have response object and on that response object again we have headers okay and again these headers are basically the dictionary so in this dictionary we are going to set the value for content type okay and we want to set it to Text slash HTML okay now let's go ahead and let's run this application and now you see that response is rendered as an HTML so instead of seeing this H2 here it is rendered as an H2 element in the browser because now the browser knows that the response which we are sending that is of type HTML okay so it is rendering that response as an HTML so this is how as a backend developer you can set response headers for the response here we are setting this content type response header so this content type is a known response header but we can also set our own custom headers for example let me go ahead and let me copy this line okay and let's say I want to create a header on the response and I want to call that header maybe my header and I want to set it to hello world okay if I go ahead and if I run this application now on the response header we should have a new header called my header and that should be set with this value hello world if I open this request again so let's make the request again and if I open the request here we have the response headers and in there we also have a new header called my header which is set with this value hello world so we can also set our own custom headers with the response so I hope from this lecture you have got a pretty good idea of what a request and response headers are and how can we access the request and response headers from our asp.net core application this is all from this lecture if you have any questions then feel free to ask it thank you for listening and have a great day
Info
Channel: procademy
Views: 3,245
Rating: undefined out of 5
Keywords: request headers, response headers, asp.net core, mvc, asp.net core tutorial, complete asp.net core mvc course
Id: ZBd_euecPBg
Channel Id: undefined
Length: 15min 55sec (955 seconds)
Published: Sat Jan 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.