How to Easily Make Post Requests in C# Using RestSharp! - C# Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody welcome back to part two of talking about how to easily make get and post requests in c sharp using resharp which is a nuget package i talk about how to install that nuget package in the first episode if you want to go back and check that out and before we get started don't forget to hit subscribe and check out my other videos if you're interested also i'm going to link this mamba wireless gaming mouse by razer in the description if you want to go check that out uh it helps out the channel if you use that link and buy anything it doesn't have to be just this mouse and i would appreciate it and today we're going to continue using the json placeholder dot i don't know if they call it typing code or what but this is the test api that you can use in your code to make sure that calling apis works before you go and implement the rest of the code right and this is what we're doing we're doing the post method and basically what's going to be returned is whatever title that we post with the request whatever body that we send with it and whatever user id we send with it and this id will be 101 and it's important to note that resources will not be really updated on the server but it will be faked as if so we're not actually updating anything on this website because you can imagine what people would end up posting but it's just a trial just to make sure your post works by the way i'm using postman and here's a good example um i did it with parameters and i also did it with body and the right way to do it is with body and postman is just an api tester it's a free software if you want to go check it out and just to show you what it's supposed to look like here is the url we're going to call our api and here are the three pieces of the body so to speak we have the title the body part and the user id of a post and then i just have test test body and one respectively so let's go ahead and send this and see what happens here you can see we get back what we put in plus the id of 101 and we get a status of 201 saying it was created okay so i'm back at my what logger demo project my logger demo project and my logger demo solution which i've been using and i use that in the last one here i have the basic outline and by the way if you're just starting on this video you can go to tools nuget package manager manage nuget packages for solution and then you can type rest sharp and install this for the project you're working on i have installed for longer demo and then you would hit install in case you didn't watch the first part okay so what i want to do is i want to send a post request rather than a get request and it's a little bit different okay so the first thing we need to think about is how are we going to send the body of the request the things that were the data that we're sending with the request with the request one way i'm going to do this is i'm going to create a new class so i'm going to right click on our project and go to add and then class and i'm just going to call this post i guess it's going to be our class name and then it's going to have three different uh properties right it's going to have title it's going to have body and user id title and body are both going to be string user id is going to be an integer so let's go ahead and well first let's make this a public class so you can see it okay and then i'm just going to type in public int and user id just as it says in the example and i'm going to add setters and getters meaning you can do it the short way like how i'm doing meaning that we can set this value and we can also get this value once this object is created okay public string and this one's going to be body and we're also going to give it getters and setters and then i'm just going to copy this and we're going to do the same with title so those are the three parts of the body that we're going to send with this request and now we have a nice little um class that we can create an object put in the values that we want for these three properties and convert it to json and send it with our request so let's go back to program dot cs here and what i want to do is i want to do request actually no i'm going to create uh our body variable or our body object and that's going to equal a new and then our class we just created so post and then i can give body a value and let's call it uh let's say this is the test body all right and then we'll go to the next one which will be title test post request and then lastly is user id now let's make that two all right so we created our new body object and then what we're going to do is we're going to add this to our request that we created up here but we're going to turn it into json so that the api can read it so we're going to do request dot add body or add json body then we're going to pass in our body object that we created up here all right so next i'm going to create our response which is going to equal client and then dot post because this is a post request and then we're going to pass in our request okay and then let's go ahead and make sure let's do console dot rate line right line not just right and i want to make sure that we get a good response back so let's uh let's yeah let's type out response dot status code and to string and then i'm going to put some spaces and then let's actually get back the content so status code if we go back to postman that was showing you earlier that would be like 201 created and now we can do content dot 2 string like we did with the get request okay i think this will work let's go ahead and let's try it out cool there it is okay so if you notice the id is 101 like we expected the body is the body that we gave this new body object and the title is also what we gave the body object and same with the user id and let's see it gave us a status that it was created so all is good so that is how you can make a post request using resharp thanks for watching guys appreciate it and i will see you in the next video
Info
Channel: Coding Under Pressure
Views: 27,640
Rating: undefined out of 5
Keywords: how to make a post request c#, easy post request c#, restsharp post request c#, how to use restsharp to make post request, make post request with body c#, api request c#
Id: fVf6wNnUSPc
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Wed Mar 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.