Getting HTTP Headers in C# is SUPER easy!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a while back i showed you how you can easily do http requests gets or posts using c-sharp and a lot of you wrote back to say that you wanted to find out how to actually get the headers from the returned response you asked for it and now i'm doing it and spoilers it's really easy let's take a look [Music] hello world i'm nick proud software engineer and big.net fan and i'm here to talk to you today about how you can get the response headers from a http request that you've done in c-sharp so this is something that you guys have requested and it comes off the back of another video where i showed you how to do a http call to an api in this case a fake placeholder api and pass the response that was sent in json so let's take a look how you can actually get the headers out of that response before we start if you like this video and you find the content useful please like and subscribe it really does help the channel so let's take a look at our code then so if you haven't already watched my previous video how to do http gets and posts in c-sharp take a look at that because that will give you a bit more background on what we're doing here but essentially we're using a net http client object to reach out to an endpoint in this case the jsonplaceholder.dipico.com endpoint for posts again just take a look at the address it will make a lot more sense it's just a an api for practicing http calls and then we're getting the result and in the previous video we then passed that result that was sent back in json to understand uh how we can do something with the response in this video we're going to take a look at getting the headers so first of all i think we should do the actual api call and then take a look at the actual headers that we received so this is a basic console application as as we used before and i'm just going to start this application so that we can see the result that comes back so i've breakpointed it here on result and then if i just skip ahead and take a look at this result object uh so you can see we've got a status 200 which is great everything's come back okay but the thing that we're actually interested in is the headers so the http response object has a headers property and if we drill into that there's lots of different things in here that we can take a look at but the things that we're really interested in are just the raw results so you just want the eye innumerable so the collection that has the headers that we're trying to get out and in your case you'll probably already have the name or key of a header in mind so for example if you were doing this http call and you wanted to get the via header you know not necessarily a header that you would typically want to get but just as an example then we would be expecting when we look for the via key to get back a value of 1.1 vega okay so that's the actual value that we want to get out so again we're looking for the via header and we want the value of that via header again in your use case it would be something different but the method of retrieving the header is the same so the first thing i want to do then is i want to create an object which will refer to the headers that were sent back in the result so i'm just simply going to create a variable called headers and that will be equal to result so that is the http result that we got here after that call dot headers so i've got something to refer to so i've got the property headers there in a variable i'm also going to create a string um called header result this is where we're going to put our final header result so assuming we get the value of our header i'm going to put it in that variable so string had a result now you can either do this or i like to do string dot empty because it's just a little bit more clear that i want to just create an empty string object so then we want to create a collection for our headers to go into uh the results of the header search that we're going to do it will become a bit more clear shortly why this is needed uh but i'm going to create an i innumerable so an i enumerable is one of the base types in c sharp it's a collection and i want to create an innumerable of string and we're going to do header search results and i'm just going to create um an a simple i enumerable with no instance i'm not going to instantiate it i'm just going to create one that just sits there waiting for something so so far what we've done is we've create created a reference to the property headers on our http result we've created an empty string that we're going to put our final result into and then we've created this i enumerable a string where we're gonna where we're gonna store our results of a search for a particular header in this case the via header that was sent in that response so then how do we get the results into this innumerable and then finally into this string this is where try get value comes into play and this is a really useful method so what i can do here if i just use the intellisense i can refer to headers so this is our headers object here and i can say try get values so let's just take a look at that so you can see in the intellisense and in the information here we can see that this function tri-get values returns a bool so what this tries to do is it looks inside the header's object and says for a value of the key that you've passed in so for us we would pass in the key via because that's the name of the header that we're looking for see if you can get any values for it and if you can [Music] put it into or output it into a variable of type i enumerable string so you can see here that's why we've created one and we've got this question mark here to say that it's nullable so put the values in there if they exist and then return a true or false to say whether they were found so in our case if the header was found it would say it would return true and it would also put the value of that header into that collection so that we'd be able to fish it out now the benefit of this means that we can react conditionally which means that we don't have to just always try and get the values and then check to see if the output was null what we can do is say if get headers.trygetvalue equals true or headers.try get values because then we can test to see if it was successful and if it was then we can get the first item that was placed into that i enumerable and that should be the value that we're looking for so let's take a look at how we do this so i'm going to place it in an if statement seeing as this returns a ball we want to evaluate if it's true or false so i'm going to say if headers dot try get values so we want to say if this is successful we're going to place it we're going to get the first one out of this uh outputted i enumerable and place it into this header result [Music] so try get values so the name of our header is via that's the one we're looking for and then we need to provide an output variable in this case header search results and so because it's an output variable it requires the out keyword first and then header search results there we go so this will now say if i was able to find a value in the headers for this key for buyer then i'll put the values into header search results this innumerable string and i'll return true and therefore the block of um code here inside the if statement will run because it's true so assuming it's true and we actually do find it then we're going to say header result equals header search results zero as in the first one now that's giving me an error uh saying that it can't apply indexing to an innumerable string which is weird i don't know why it would have suggested to do that um so what we want to do is use first because that will just give us the first item that was outputted into that innumerable you can do first or default if you like and then check to see if it's not later although yeah that should be fine but really because we've used tri-get values and that returns a true or false if it's true then we can pretty safely assume that it's placed a value into header search results into that i enumerable so if all goes to plan and we do get a value out for this header here it will place it into this header result variable so let's give this a try and see what we get so i'll start the console application again and then we'll get the results we've got headers um and then we've created our blank header result string variable then we're going to do our headers.try get values and you can see that has actually returned true so if we take a look before we go any further at the ironnumerable you can see there is one entry in there and if we just take a look here we can see the first entry is the value we were expecting so you can see now that it's going to assign it to that header result and there we go all good let's try this with another variable with another header then so let's take a look at some of the other headers that we've got just to hit this home a bit more so we'll go to the results say we wanted to get the the date so here's the date value and it's under the key of date so there's a header in there called date so we can use the same method to grab the date header so we could say we'll get it as a string date had a result called string dot empty and then we can say if headers dot try get values date we want to put it out into header search results again we can just reinstantiate that collection with the result then date had a result equals had a search result start first there we go so we'll try that see what that gives us so it does our first one and you can see here that it has worked for the date as well and it's inputted the value of that date into the iron numerable that we can then fish out with first if we take a look at date header result we have the long date in string format and then you can then pass that date and do some other things with it if you want to but the key thing here is that we were very easily able to grab the header out and if it doesn't exist then it will just return false and you can evaluate that and do something with it so it still allows you to handle that if you were expecting a header for example to exist and it doesn't you can put some contingency in place to say if headers dot try get values equals false or you could append an else to these if statements for example then you can still handle that because you were expecting a header that didn't exist so you can see a very very simple method with one function try get values it's a very good way to grab header content out of http responses if you found this useful please like and subscribe it really helps the channel i really enjoy doing these videos and i really hope this is very useful for you and just let me know how you get on like if there are any other things you want to look at in terms of using the http client in c-sharp then i'll be more than happy to do more videos on it until next time keep coding [Music] [Music]
Info
Channel: Nick Proud
Views: 14,494
Rating: undefined out of 5
Keywords: .net core, REST, REST API, api, apis, c#, c# http request, c# httpclient, c# lessons, c# projects, c# tutorial, c# tutorial for beginners, cloud development, developers, dotnet5, dotnetcore, headers, http, http header injection, http request c#, httpclient c#, https, microsoft, prc, programming, rest, rest api interview questions, rest api tutorial, restsharp post example c#, sdk, software development, update, upgrade, web, web development
Id: NvT5QRTpys4
Channel Id: undefined
Length: 13min 14sec (794 seconds)
Published: Tue Apr 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.