How to CALL POST API in C#! - THIS EASY!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to tutorial CU in this video I'm gonna show you how you can send a post request to a public API and for our demonstration we will use the Json placeholder API where we can actually just send a simple post request to create any kind of record [Music] just for your interest the entire video is also written as an article at our website and you can find the link for that in the description below so this is where you can well take a look again and copy the code to follow along and all of that so making post requests to an API is definitely a common task when working with web services in c-sharp so first of all we need to think about what exactly do we want to send and what we will get in return so what kind of response do we have so here we have a clean.net 7 application a console application and I'm now going to create two new classes so I simply right click in the project add a new class and I will call that post data and this is basically what we want to send to the post endpoint of the API so posting for sure is for creating data right so let's say I want to create it with the post I upload the YouTube video or whatever all of this is really a post request because post requests are creating entities in the backend so this is our first class and inside here I will just copy paste that because I don't want to borrow it too much I would just create that class class post data and inside here we got three properties right string name and age and string address this is basically what we want to send to the API endpoint now next up the API will return a response right after we send something to it so first of all we send some data to the API endpoint and then the API will send us a response in return so I'm going to create a new class now and I will call that post response you can call that however you like but I just want to make it as clear as possible and again I want to copy over some code here and it's called class post response for sure and we get a property public int ID so this is just what the testing API that we are using will return so we are going to create something and then we will get an i ID for that created entity in return now it really depends on what you want to use as an API so if you have your own API or you have a well let's say weather API or whatever the response could be like um some other kind of information not only an ID could also be like any kind of message or whatever or URL that points to the created entity or whatever you can really think of so it is specified by the API itself so you have to check that next up we will create our HTTP client object and we will set the API based address so let's go to our program.cs now first of all I want to make that class is public here so public class post data you go I also want to make it for this one so that I can access it from everywhere and then I want to go into program now inside of our main here I will just remove that code and I will now create a new post data so VAR post data this is what we want to send to our API as I said and we want to configure it here so let's say the name for example John dough and we got the H set to 30 and we get the address set to let's say one two three main street or whatever you know let me just add a semicolon and then we get the post data ready now let's set up a client so let's say VAR client equals to new HTTP client this is the important part right let's take a look let's hover above it you can see if I just click on that it will open up the HTTP client and you can see that this is coming from the system.net HTTP namespace so this is something really related to pure.net framework now we need to configure the API Base address for this we are going to set the client dot Base address to whatever our URL of the API is so we're going to create a new URI here and inside here we simply paste the link so for our Json placeholder API it is basically that one https jsonplay solder.typical.com awesome now let's take our post data object and transform it into Json because we want to send Json to an API so we take our pulse data object transform it into Json and we send that Json as a string to the API endpoint so let's say VAR Json and we want to use the system.txt namespace so let's just scroll up let's say using system dot text first one and using system dot text Dot Json awesome let's scroll down again and now let's say Json serializer dots realize so we're gonna convert a C sharp object into Json and we want to convert our post data object here so let's paste it inside here now we get Json now we will create string content that we can send to the API endpoint so let's create a new string content object there you go let's say Json and we want to use some utf-8 encoding so encoding Dot utf-8 and then we want to set the header for the media type and the type that we're going to submit is application slash Json so basically we are going to say that the information that we're sending comes in Json formatting this is what we're doing here great so now we can use that content object here to really post it to the API so let's make a final call let's say VAR response so this is what the API will send us back when we call client dot post async so this is the actual post method that we're going to use to send information to the API endpoint we're gonna say we want to reach the end point which is called post so this is also specified by the API itself so the endpoint will be https jsonplaceolder.typical.com posts this is as I said the endpoint specified by this API for your well other apis whichever you want to use it might be different but this is the actual endpoint we got the base URI here and then we got the endpoint each API or every API has endpoints where you can send your data grab your data delete data and all of that now what do we want to post there we want to post our content there our string content object awesome and then as you can see here response right we will get right after the asynchronous call is done so post async we will get a result so this result is our response so whatever our API has sent us back in return before we continue I want to tell you that we have a unique online course which is called the seashell progress Academy that teaches you full stack sing shop development so asp.net core angular test driven development c-sharp software design patterns and so much more and I'm 100 sure that this is the best way on how you can progress as fast as possible as a c-shot developer you can find the link in the description below or popping up right now at the top right corner so right now we're getting the response of the API let's take a look so let's see what response we're getting first of all let's check what response status code we got so is success status code we're gonna check that and I will explain what it does right now let's create an else statement here too so when you send a request to an API you will get a response and this response can have well different status codes everything was in a range of 200 is a success status code so let's say 200 that would typically be the OK status code which means okay your request was successful everything was related to 4 or 500 is an error 500 is a server error on 400 is most of the time a client error so 404 for example is not found and 500 is a bad request something went wrong on the server so if I hover above that you can see State this code was in the range of 200 to 299 then it's success so our request was successful so let's take care of the else statement first so let's say C W console right line here in that case we will have an arrow because it's not a success status code so we can simply lock the response dot status code awesome so now we have that done let's take a look at this success part first of all what we want to do is we want to read the content again as a string so we're going to say response content equals to we take the response here we want to take the content of the response and we want to read it as a string so read as string async and since this is async we should wait until we have the result so even though this is like asynchronous right now we are writing it down synchronously because we're blocking because we're waiting for the result but that's fine it's just about the theory here now we got that done what we want to do is we want to turn that into a c-sharp object again but before we do that we want to console right line again here to just check the response content real quick so let's start the application this should send a post request to the API and we should get a response and we should see the actual response of the post API request that we send so we can see this is what the API answered name John h30 address blah blah blah and then we got the ID so this is basically the response and yes it got successfully created awesome so one of the most common questions is like how can I turn the answer the response again into a c-sharp object and for that we will simply use the well completely opposite of our serialize we will use deserialize so let's just say that we have let me just add that here in between VAR post response we want to turn it into our post response object here that one response right we're going to use our Json serializer again but this time we want to deserialize and we want to deserialize into the type of post response so let me just zoom in here post response and what do we want to deserialize we want to deserialize the response content from that line above here awesome so finally we can simply say ID and then say Reese post response our c-sharp object dot ID now let's save it again let's hit run and we should now see any ID which is coming from a true C sharp object so the application has started and now as you can see we get ID 0. so why is that happening and this is something a lot of beginners are facing again it is because it is case sensitive so if we again maybe you didn't notice it if we take a look at our response content again just real quick so let me start that again take a look at the response that we are getting from the API so can you see that name age address and ID name agent address are in capital letters and ID has a lower letter here so if we now close that application let me just control that here let me change that into ID in lowercase post response we're going to do ID in lowercase here too we start our application again you will see that we will have a value for the ID you can see that it now says id101 so when we use the lowercase it starts working because we are able to deserialize into the post response now we don't want to have that because like properties should not be written in lowercase let's just undo that let me show you how you can fix that easily and we can use some options to fix that issue so let me just copy and paste some code here there we go we can paste it here we can use options Json serializer options and let's say property name case insensitive is set to true now we want to use these options in our D serialize here so add it after our response content options right there we go now let's start the application again and as you can see we now have an ID and our c-sharp code is still following the correct naming conventions awesome so yeah this is how you can send a post request to an API and how you can read the response if you like that video make sure to subscribe to our channel to no longer miss any upcoming videos on c-sharp and for sure check out our C sharp progress Academy which will turn you into a full stack c-sharp developer in basically no time foreign [Music]
Info
Channel: tutorialsEU - C#
Views: 54,825
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, Step by step, guide, development, programmer, video course, video tutorial, learn how to, how to, visual studio, c#, .net, .net core, dotnet, visual studio 2019, core, code, asp, asp net, c sharp, coding, csharp, programming, api, rest api, rest, json, http, api testing, software development, c# tutorial for beginners, c# tutorial, c# restsharp tutorial, how to call post api in c#, csharp api, step by step, mvc, web api, rest api tutorial
Id: ufHlJLPK5CA
Channel Id: undefined
Length: 14min 14sec (854 seconds)
Published: Thu Jan 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.