POST Method in c# | Headers | Send Async | #7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to binary automation so in this tutorial we'll see how we can send a post request using the http client class so we'll basically using a method called send a sync this is the most common and the most generic method that everyone uses uh since we can send any kind of http method right like your post port delete right and uh along with that we'll also look at what are the different headers how we can pass headers and how we can send headers in terms of the key value pairs and get the headers in the response as well yeah so let's get started so i have a sample post request with me um so basically this is the request only this is the url that i have and if you see the sample json payload it has id quantity price and if you see the response it's basically it's a very simple response success is equal to true and if you see the headers that we have the response headers we have date content type content length connection set cookies and a lot more headers there right so let's see how this can be done right so i first i'll be writing a classic vanilla code within our main function i'll be writing the entire code but uh you know you can just take bits and bytes and put it in a method and parameterize it but for simplicity i'll just write this in the main method yeah so first i'll just initialize some of our http client class and we'll also be needing a http request message and shtp response message so all this comes under the same package right uh classsystem.net.http.hlp client right so all are under the dotnet now let's uh let's also initial let's also create a variable for a string called response body now let's begin by creating the object of the http client class here and also create the object of the http request message basically request is equal to new http request message so if if if i show you the documentation or official documentation of the http request message uh it basically requires two two values here right one is what what is the http method you want to pass basically put post delete and then what is the uri basically what uri you are testing right the end point that you're trying to test so uh let me just give the http method as as post and the uri that i'm trying to test is let's get the uri we need to also have the request json request payload uh send along with the post message right so in order to do that uh we have uh we are creating a data model actually uh so basically i have a class called data send and uh within that uh i've initialized three properties here id quantity and price so if you look if you look at the uh sample um json right uh it has id quantity and price so that's exactly what i'm doing here so we need to uh serialize our data now right in order to se in order to construct and send that within the as part of the sender sync right so let me just uh use the json convert okay and json convert dot serialize so i have to import this newtons of json right so basically this is the nuget package that i've added and basically you have to use that to serialize your data new uh the class is data send yeah new data send right so basically your id uh you can give some value your id is 156 your quantity is 1 and your price is like 15 yeah when we have the data in certain string formats we need to convert that into http content in order to send that with uh as part of the sender sync method or the post or sync method right so uh let's wrap it up now wrap it within the string content word data string content equal to new string content so the data that i have to pass here is yeah but before that i have to just put this after initialize the string uh the one that was there was just serialized right uh so let me just put this in the string content and along with that you can initialize your string content with certain additional values right uh for example if you want the encoding here right and what kind of uh whether whether we are sending it in terms of xml or json right so let me just add this as the encoding will be encoding dot qtf8 i mean according to your projects you can have the relevant encoding right so encoding.utf8 and i can pass like application slash json okay hope that makes sense now now since we have constructed our json payload or request payload now we need to just attach that to our http request message yeah so the way you do is uh http so basically the request that you have just created here request request.content equal to string content yeah so basically you're just passing that string content to your request.content yeah so now let's start now by adding the headers to this request right request content that we have let's try adding headers now so now headers can be added in multiple fashions right so you can you can use for example the http client like for example we have the client dot default request headers and you can you can just do an ad here right you can add this in terms of the key value pass that's that's one of the option that you have uh that's a couple of other options but what i prefer like some of the people who work in the industry they prefer is they kind of uh use the headers or add the herders rather in a in a class which is in build called name value header value class right so what it does is that you can add the headers in terms of keys and values and then and you can even run a for each on on top of that right so let's see how it how it can be done right you can even you can even actually um have a dictionary in li initialize and add that and then later add that to the request.headers so so let me just show you how it can be done so i have a list uh with name value header value right so if you see this name value dot header value that's what exactly that's exactly what i'm going to use okay so this name value header value comes within the system.net header http headers name value header value so if i if i show you the the official documentation for this right uh basically represents a name value pair used in the various headers right so if you see the two properties that we get uh for this name and value right so you can just uh retrieve it by saying you know the the name value dot like name and the value so you will get the those relevant details so let's see how we can add certain uh headers right so if for example let's rename let's rename this as list headers yeah for simplicity and we'll have um so let's add this list headers yeah whatever let's headers dot add okay we get an add method here so if you see you can we can actually create this object of the new name value and we can pass the headers here so say the content is like encoding right utf-8 and similarly you can pass multiple such values right let me just add another value here as like user agent and pass certain machine name or something like x machine okay yeah and so now let's see how we can actually add this to our actual uh request dot headers right so that is basically what we want right so i have of let me just write it for each okay and uh so let me just pass this list headers here and give this as header so basically within our request request dot headers dot add okay so if you see this request dot headers dot add right so you'll be able to add the headers in terms of your key value right the header dot value header.name and header.value basically what we're doing is we're just looping this uh the list here and then just adding those in terms of adding this uh in in terms of header.name and header.value so your request.headers is now initialized with your headers right so that that's really cool right we have the headers and now we have the content right your request content now if you want to send this as part of the sender sync it's so all you have to do is client dot sender sync what you can do is you can send the request here and this will actually give you a response so let me just show you the what the sender sync requires right sender sync requires an http request http request message so that's exactly what we are sending and the error that you're seeing now is because we have to wrap this with the with the await and a sync model right so since we are writing an asynchronous way uh we need to have we need to wrap this in the uh await in a sync method right so i've already added the sync here and the task right so let's let's just uh proceed ahead right so now we've got the response now let's have this response body in write it as a string right so await response dot content dot read a string right so what so what this does is it will give you uh it will just uh give back the whole thing in in the string right and then you can do whatever you want with the string right you can for example now let's just now let's just print this okay now once we send this let's let's do another thing the the additional part that we want to do right we'll also see how we can uh get the headers from the from the response and uh how the headers are going to be right but before that let's just run it once and see how the whole response looks like yeah okay great right we've got success as true and that is exactly what we wanted right so uh let's just run this uh in terms of how we can uh get the headers in the response as well yeah [Music] so we'll be writing a for each loop now okay for each uh war item say in response dot response dot headers yeah so it's exactly very similar to what we have done earlier right so basically when we wanted to add something we uh we added that in the in the request.header so if you see this request or headers we just added that and and the same thing applies to your response message so if you want to retrieve what are the headers and you want to assert something you can just do a response.headers and just retrieve the the values right so so now for example i want to display or i want to print the values what i can do is write line i can say that key is okay uh item dot key right and uh let me just append this and and add the remaining part of you know what the value would be right the value is item dot value now i'll just show this part here why are we using this first or default or thing right right so if you see this carefully what this response.headers return right now response.header basically gets the collection of the http response headers okay now what what does it actually return the hd response headers right so so let me just show you this now what kind of error it will throw see this kenwar cannot convert type system collections generic key value pair in terms of string so here your string is nothing but your header name okay and the value is in terms of i need i innumerable string right so basically you will have a string it's kind of a string wherein you can loop through the string right you can have like string separated uh it's like a street you can have string arrays or you can you can actually enumerate on on top of that right so uh let's just uh put this back as our item and that's the reason we are we are doing this right so if you see the dot item.value what it returns it returns i i enumerable string and from there uh you know the so if you see this first or default there is an extension method on the string part and it returns the first element of the sequence or default default wall value if the sequence contains no element so we have a similar method which is called the first but the problem with the first is that uh in case if it is uh like if it is null or blank uh it can throw exceptions as well right so that's the reason we are using the first or default great so let me just run this for you yeah right great so see if you see this key is date date or let me just uh add proper spaces here so it'll be easier to okay and run it again so if you see the key key is date value and that throws the actual value um the key is blind block connect value keep alive access control allow origin value is just a star um the key is cf cache status value uh and the value is dynamic so you get all the response right so now you can take this individual keys or individual values and and you want to do it or you want to save it similarly you know you can you can even have the authorization for example you've got certain um tokenization in the headers headers and you want to validate that right so you can maybe a better token or some other token and you want to see how it works right so this exactly works exactly works like that right so even even in the in the top one that we saw right where we were in we were trying to add the headers request headers you can actually loop through the headers in this in the similar fashion that we have done below right so all you need to do is uh over here in terms of uh response right headers you will just do request dot headers and you will just print it so let me just do that for you as well if you want to print what was the request headers right so request i'll just say request dot headers and i'll print this as well for you right so let me just run this okay if you see the last two uh keys encoding values utf-8 and user agenda is x machine right so this is how you can just traverse through your headers your response headers and your request edits so hope you like the video uh please um comment on what further needs to be uh you know talked about or or made a video on and uh please comment likes and please share the video across so that you know i can gain more um visibility thanks a lot
Info
Channel: Binary Automation - SDETs
Views: 5,865
Rating: undefined out of 5
Keywords: post request in c#, specflow api testing, api testing with c#, send async, httpclient, api testing, c# api automation, specflow c# api, test automation, http, request, how to send post method, http headers, response header, validate response json, httpRequestmessage, httpresponsemessage, serialize json, deserialize, serialize objects c#, GET method, get request in c#, send post call with headers in c#, post call c#, post request headers c#, add headers in keyvalue pair c# post
Id: dlwwz4SCQkQ
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Wed Aug 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.