Built in REST CLIENT (.http/.rest) file in Visual Studio 2022 | Best way to test the API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone my name is nitish Kumar and in this video I'm gonna talk about a new feature that has been added in Visual Studio especially for backend developers whenever we work on any back-end project for example the rest apis or the micro Services then during the development to test them we have to use some external tools for examples we can use Swagger we can use Postman and there are so many similar tools available to test these apis I personally use Postman very much to test all my apis during the development but now we have something new and very interesting let's see how it will work here I have a very basic esp.net core web API application and in this API application I have created One controller so this is the employee controller and in this employee controller I have added couple of endpoints the first endpoint is this get so if I will hit this API then I will get this hard-coded response the purpose of this video is not to tell about the esp.net core functionality but it is about how to test them and that's why I'm using the hard-coded data the second API is again a get API and here we can pass the ID and just for the testing purpose I am returning the same ID here in this output message and here again in this post API we are getting the data and we are simply returning them now we have one more put API and in this put API we are having this ID and we are also having the value we will work on all of them in this video so first let's run this application and here you can see that we are getting this Swagger window so if I want to test them I can simply click on this link again I have to click on this try it out and click on this execute button and here you can notice we are getting this output which is value 1 value 2. but here I am not interested in this Swagger I am interested something new that has been added in visual studio and this new is that we can add some files in this visual studio and we can directly test our application or the endpoints from that file let's learn how to use this feature to work on this feature simply you have to create one new file in your solution now we have the question where should I add this new file so again it depends on your application you can also add it in the root directory of your application or you can also create one new folder and inside that new folders you can add multiple files so here let's add a new folder so this is the add folder and here let's say I'm writing Rest tool now inside this folder we have to add a new file so right click on this folder go to this ad and we have to click on this new item and over here on this add new item window you can give any meaningful name to your file for example here I am having employee employee rest and here is the very important part the extension of this file can be either rest or EST or the HTTP so let's say here I'm writing HTTP the name of the file does not matter but the extension definitely matters let's click on this add button now this is a very special file in this visual studio now I can test all my apis of this application by using this file let's see how it will work so first of all I have to write what is the verb of my endpoint the verb is HTTP get put post the methods that we generally have so let's say I want to test the get API and remember you are getting the intellisense as well we are having the intelligence and we are having all the methods over here so let's say I'm using this get okay let's run this application and I need this base URL of this application so after this get we have to give any space and then just paste this URL now we have to enter the exact URL for the endpoint and we are inside this employee controller so we have to write API employee and that's it because first I want to hit the first one so here I'm writing API that's it immediately you can see we are getting a green symbol over here let's click on this one here you can see on the right hand side we are getting something very interesting it is saying the response time is one three six millisecond the status code is 200 the transfer encoding chunked date this is the date this is the server and this is the content type and this is the actual content what we are doing over here is we are calling this endpoint with get method and this is the output that we are getting and because in the development we have to debug our code a lot so I want to test whether the debugger works or not so inside this employee controller I want to put like this and from this file I'm gonna click on this green button again you can see we are getting this debugger at this particular line so we can execute it line by line like this or we can also click on this continue button and here is the output that we are getting now let's stop the debugging mode I just want to test what will happen if I will click on this green button now you can see we are not getting any kind of output and here in the bottom side of this Visual Studio you can see that it is sending a request to this particular URL so after some time you will see the request is completed and this is the output it is saying status code Gateway timeout it means this application is not running and we are getting the timeout error at this place let's click on this one and we are getting the debugger over here and this is the output this one okay now just for the confirmation let's update some of the data over here and here I want to add some Demi data like this testing save the changes run this application let's remove this debugger and and from this HTTP file let's click on this green button and here you can see we are getting the exact output from the API this was the very simple example here we are not passing any kind of data but now let's understand what will happen if I want to pass some data as well it means now I want to write some code for the second method as well over here how to do that to add a new method first of all you have to add a line break and to add a line break in this file you can use hash symbol three times like this now you can write the code of your second method so let's copy this one and just paste it again now I want to test this second method so what is the URL it is API then the employee and then we have to pass an ID and let's say that here I'm passing one so ideally what is the output we are getting the ID over here and we are simply returning it with this message this is the get API for ID is equals to ID whatever data we will get in this parameter we are simply returning it let's run this application and here if you can notice we are getting this green symbol in front of this method as well so click on this one and you can see this is the output from the second endpoint this is the get API for IOD is equals to 1. you can modify your data at the runtime as well so remember I am still in debugging mode and I'm just simply updating the data click on this button again and here you can see we are getting the second output if I will click on the first one then I am getting the data from the first method if I'm clicking on the second one then I am getting the data from the second method this is very interesting and very useful now here comes our third method and this is the post method we understand how to pass the data in url but how will you pass the data in the body again I'm gonna use this hash symbol three times to add a line break and this time I'm interested in the post method so let's just type this post and what is the URL the URL is this same this one because there is nothing in the route so it means it is API then the controller and the method is post so here in the body we are passing this employee model what is this employee model this employee model has three properties in the post method because we will send the data in the Json form so we have to update our headers as well to add a new header in this case we have to go to the next line like this and here we can type the key and the value of that header content type and here you can see we are getting the intelligence as well so this is the content type application Json like this this is how we can pass the data in the header of this call if you need to send multiple headers then again you can add a new line and here you can type your header but now let's talk about the data that we will send in the body to add that data we have to add one blank line like this so see this is the blank line that I'm having and over here I can pass my data like this so first is ID what is the ID let's say it is 1. then we have let's see what all properties we have in this employee model so we have ID we have name and we have the email so this is the name this Cosmic and what is the email testing at the rate sample.com this is very interesting okay now let's run this application over here and I want to test how this third API will behave so click on this green button and you can see on the right hand side we are getting this output whatever data we are passing we are getting the same as it is over here if I will update it to 10 and just hit on the screen button again see we are getting the tan output if I will update this value and click on this again button we are getting the updated value why we are getting this data in the output we are getting it because we are simply returning it from this Excel method just for the testing purpose I want to update this value so I want to write if value is not equals to null then I want to just update the ID so for example it is value dot ID plus equal to so whatever data we will get over here in this employee model I am just simply adding 2 in the ID let's run it again and from this file let's click on this green button this time you will see we are getting 12 over here if I'm sending only 0 click on this button you can see we are getting 2 here in the output this is how you can send the data in the URL and this is how you can send the data in the body let's recall this post method very quickly let's recall this post method very quickly so first we are having this verb then we are having the URL then we are having all the headers line by line so I am using here only one header but in your application if you need to pass more than one header for example authorization then you can also use all of them see we are getting the intelligence as well and here you can type your header let's quickly test the put API as well so in this port API what I'm having is I am sending the data in the ID and I am also sending the data in the body so it means I'm sending the data at two places let's quickly just copy it and just paste it again like this and this time I have to use the put method and here in the URL I need to send some data for example it is 10. and in the body what we are sending we are sending only a string value so we are not having this model in case if you are using the model then you can send the data like this but I want to test some other types as well so this is a very simple plane string and here I'm typing nitish cosic the save all the changes and over here I want to put a breakpoint like this and from this HTTP file I am clicking on this button see what we have in this ID we are having 10 and what we have in the value we are having the exact value that we are passing in the body from our HTTP file let's click on this continue button and this is how we can pass data in the URL and in the body let's talk about some more features we are using this has symbol three times just to add a line break but what will happen if I want to add a new comment over here for example I want to Define that this API is used for this purpose and the second API is used for some another purpose how to do that to add a new comment in this file you can simply use the hash symbol like this get all employees like this and over here I can also write one hash symbol get employee by using the ID like this I can also write my comment along with these three hashes like add a new API and again we can have n number of lines over here for the command so let's say here I want to add one more comment pass the editor in the body again I want to add one more comment make sure to pass the correct content type like this this is how we can add the comments in this file now if you can notice over here we are using this same code multiple times we are just duplicating it you can use some more features to organize this file in a better way let's say I want to add some variables in this file so how to do that so first of all I'm adding a new line over here and before that I want to create some variables to add the new variable I can simply use this at the Red symbol and I can Define the name of my variable for example it is ID to assign the value we have to put the equal symbol like this so what we have done over here is we are just defining a very simple variable and now I want to use this variable at multiple points here you can notice that we are passing some data in the URL instead of passing this hard-coded data I want to pass the data from this variable to use this variable over here we have to use this curly brushes two times like this and we can simply use this ID that's it just save the changes and again let's copy it and paste it at this place as well so what is the benefit now we are using the variables and if I want to test this data multiple times for this variable then I have to update the value only at one place I'm using it multiple times see now let's just test how it will work let's click on this button you can see we are getting some error at this place it is saying this 10 with semicolon is not valid it means you cannot add the semicolon over here and I have added this semicolon purposefully because we are having the habit of adding the semicolon at the end of every line remember you do not have to add the semicolon at this place now let's click on this green button and see we are getting this output over here if I will put a breakpoint let's say I'm putting the breakpoint over here in this put API and I want to test this put API click on this one see we are hitting the debugger and we are getting the proper data I can also update any data over here like this let's say I am having this 100 and let's just test it this time I will get the 100 value over here now let's talk about our second variable so here let's say I'm having the base URL and over here I want to add this base URL I am not using any kind of semicolon or double quotes or single quotes over here now I have to use this base URL how to do that like this and just the base URL that's it let's use it at all the places like this here as well in this post API and here as well in this put API let's run this application click on the first one you can see we are getting the data from the first endpoint click on the second one we are getting the data third one we are again getting the data and on the last one we are also getting the data from this put API the best example of this variable is authorization token the bearer token that we generalize and is very large you cannot write that token every time in this API and for that you can simply create a new variable over here with any name let's say it is token and here you can write your token and you can simply use that token over here in this header remember this variable will work in the header as well you can also add your file with extension dot rest let's add a new file with rest extension so here I am adding add new item here we can have the common name or a different name as well just for the consistency I'm using the same name so it is employee rest dot rest like this click on this add button and we will get this new page let's copy this enter code and paste it in the rest file see we are getting all the symbols over here if I will run this application we can test all of them from this file as well this is the value again this is the value so either you can use this rest or you can use this HTTP I seriously believe that this is a very important feature for the backend developers now you can add a different file for all your controllers and all the developers can test them from their own system at this point I would suggest not to compare this tool with the mature tools like the postman or swaggers this is just the starting point for the backend developers in the visual studio and I believe testing your code by using this file is much better and much faster as compared to those tools that is all in this video what do you think about this new feature for the backend developers let me know your feedback in the comment section below thank you for watching have a great day thank you foreign
Info
Channel: Nitish Kaushik
Views: 40,461
Rating: undefined out of 5
Keywords: .http file in visual studio, .rest file in visual studio, http file in visual studio, http file in visual studio 2022, rest file in visual studio 2022, rest file in visual studio, built in rest tool in visual studio 2022, API development inner-loop, rest client visual studio, rest client visual studio 2022, rest client c#, built in rest client, built in rest client visual studio 2022, what's new in visual studio 2022, what's new in visual studio 17.5, api testing, http requests
Id: t75Uz515WVg
Channel Id: undefined
Length: 18min 16sec (1096 seconds)
Published: Tue Mar 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.