Using http Files in Visual Studio to Test and Document APIs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
testing and documenting apis in Visual Studio is even easier thanks to the latest update now I can use HTTP files to execute an API call and see the results right in our editor let's see how they work in this 10 minute training video now for most of my training I work to give an in-depth perspective on technology but sometimes you just need a quick answer the question how do I use this that's why I created this 10 minute training Series so let's dive right into the code so what I have here is a quick minimal API that's just a little demo to show off what we can do in API so we have a people list here again three people right in line there's no data access or anything and then down below I've gotten rid of the Microsoft's default or sample code and I've added four endpoints so I have the get post put and delete endpoints and these are just a show um what can be done okay what what how to execute different calls using this HTTP file so if we were to run this and let's do this we can see what it looks like wait for it to execute and we have our git post put and delete and of course we could load this up and try it out with swagger and and see execute and go create you know that works and and do that all the rest of these and it shows you how to build out the various things we need to in Swagger but we want to go beyond that and learn how to use this new HTTP file because this works not just with your own apis both any API including API that's external to you your company so let's just say we're using an API and you want to document how it works or even test how it works well let's do that now so I'm going to create a folder I'm going to call it docs just because um I like to you know organize these things I would probably put in some type of folder I'm not sure yet in the naming convention but something like docs or something like that and then in here I would add a new file and I do one per endpoint type so I'd call this the uh the people uh dot http okay and we hit OK and here's a template for that people.http and it says hey if you want more information go and visit this URL sorry to get rid of that and what I do is build out a few calls but the first thing I'm going to do is we're going to look at the properties and launch settings and down here we see that this is our base URL so I'm going to copy that base URL we're going to need that um for all of our calls so let's actually make a variable here we'll call it root URL and just paste in no quotes and what that does is it gives us a variable we can use to then say get and double query raises root URL slash people and now we have is we have a call set up to actually test our API now it's a little green arrow to the left of it this would execute it in fact I can click this and it's going to execute the problem is is that right now you know we have a 504 Gateway timeout because it tried it couldn't find anything because we don't have the API running we're testing our own API the API were already inside so you have to have this API running in order to run this test but if we were testing out uh the the swappy.dev API which is the Star Wars API you wouldn't need to have anything running because it would work just fine uh with nothing running but let's let's run this API I was push it off to the side we don't actually need to see it um it's running now if I were to run this again notice now we get a 200 message and we get our content that says it's an array of of objects Tim Corey Sue storm and John Smith IDs one two and three so there's our there's our call to our API we have other types here as well so let's see how we get around calling these other types like a post or a put so to separate out your calls put three hashtags in a row this means this is a new call to an API where I say this will be a post and we'll do our root URL slash people again but this time we're going to say the content type is going to be application slash Json you can put a lot of you know all the the header stuff that kind of stuff in here and I'm going to put in the body just by putting clear braces here let's say ID is four now this is Json first name is Jane and then last name oops is uh Jones how about that so what I've done here let's put our I like to always close out my call afterwards um what we've done here is that we're gonna add post command a post call which we're going to pass in a a body that has a an object in fact we come back over here and look um and our post command we are passing in a person model object and we're adding that to the people list if you look at person model known as capital I for ID capital F and first name capital L and last name and yet that's not what I'm doing over here because C sharp does translate these things from Json and the normal Json convention over to be the c-sharp convention so this would be typical in how you'd call this so let's run this again let's say just run post and it says you got 200 messages if we run the get again we can now see we have a fourth entry one for Jane Jones but let's keep going let's look at what a put command would be and so we have our root URL slash people slash let's update number four just uh because and we'll say content type is application slash Json and for this put command what I did was we looked back over here I pass in an ID on the URL but then it also pass in in the body the person model and I say hey let's find that that one and if it's not null if you found it then update the first and last name based upon the model you passed in so with that let's pass in let's say the same object and we're going to say Jane Smith instead we'll change it from uh Jane Jones to Jane Smith let's run this put command that ran it returns back the object that we just built and then we're updated we come back over here notice that Jane Smith is now id4 instead of Jane Jones and last but not least let's look at how we delete something so let's close out that command and say delete root URL slash people slash four and again let's just close that command out and we're going to execute this and we get the 200 content is true which if we go back up here to our get command we can see we only have three entries now not four so that's how you use the HTTP file to test out your API or test another API also document your API because let's save this when you come in here and say hey how is this API work well I can come over here to the people.http and see all different commands and an example of how to call those commands so there's a lot of different ways you can use these HTTP files for documentation for testing for figure out how an API works or play around with things we can use variables like this and of course they pass through all the different calls and we can separate out by file for different endpoint groups that way you can have one for people and one for employees and one for you know whatever the the different paths are of your API so lots of cool stuff with this there's a lot more to play with there's a lot more depth and this is a newer feature to visual studio so there are still some things they're working on adding in for the more complex uh use cases but I would encourage you to try this out and let me know what you think let me know if you're going to add this to your um your API not really as a unit test but as a way to test out and try out various endpoints and also to document your endpoints thanks for watching and as always I am Tim Corey [Music] [Applause] [Music]
Info
Channel: IAmTimCorey
Views: 20,141
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6
Id: drwc2dF3u7I
Channel Id: undefined
Length: 9min 36sec (576 seconds)
Published: Mon Jun 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.