Using http Files in Visual Studio to Test and Document APIs
Vložit
- čas přidán 4. 06. 2023
- Testing and documenting APIs in Visual Studio is even easier thanks to the latest update. We can now 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.
Full Training Courses: IAmTimCorey.com
Mailing List: signup.iamtimcorey.com/
Thank you TIM. Already using it. Personally someone who does not like to go out from IDE during development , this is a great feature.
You are welcome.
I actually used this today. Was easier than dealing with postman. Thanks.
You are welcome.
Thank you. This will save untold amounts of drudgery of dealing with Swagger pages.
You are welcome.
Thanks Tim, this is huge! I have already started to port my "Postman" stuff to http files. Http files will become the norm most probably!
You are welcome.
@@Luke5100 authentication works like a charm - you put the token at the beginning. It depends on the authentication scheme obviously.
HTTP files are really cool. Awesome video
Thank you!
I can see this as documenting to other developers how to use a feature or as a testing suite for them to follow.
each call is a step that they execute (create an account, get resources, manipulate them, and delete at the end). but it then becomes another duplicate of what should be part integration/e2e tests. and then it goes out of sync and we end up with more files in the source code that nobody updates until something breaks.
I think this will end up like having scripts in the source control. they are used by two or three people, and then those people leave and nobody knows what to do with the files anymore.
A very nice. So much easier than writing integration-tests. And a lot smoother than having seperate individual postman projects. 👍
Thank you!
@@IAmTimCorey Hi. Visual Studio now comes a built in view called Endpoints Explorer which allows you to generate the requests 👍👍
Thanks for the video. The question is how about getting the first person without knowing its ID (in this case that backend generates GUID) and use that ID in the next request ?
That looks like a great way to keep your calls with the app so when QA comes knocking, you can hand them a list.
Yep.
Do you know if they will be adding a way to add comments to the .http files? Maybe supporting markdown in those comments? I'm imagining keeping all of an API's documentation in .http files, with separate use cases for individual (and series of) endpoints, each described in depth within the comments, then demonstrated using custom urls and bodies, each with their own little green button.
Nice feature. Is content type form-data supported? I've tried but could't be successful.
How can I test posting a form file? I'm getting an error "Failed to read the request form. Invalid header line: asd".
if we can write more code in those {{}} we could even capture data or tokens in variables, run in loops, add delays, and write entire tests lol, but i think at that point it would be another project in CS more appropriately but, it would be nice to write a littttle bit of code here for documentation purposes, its cool.
Can you upload source code of this project?
Nice! I used to use a VS Code extension, Rest Client, that offers the same experience.
Thanks for sharing!
Yes, this is based on the VS Code plugin.
Looks like a nice feature.
I like it.
Love it, thanks 👍
You are welcome.
Great, but is there a way we can integrate it into unit tests, that gets run and return the pass / fail?
Not yet (with yet being the key word). It isn't really designed for that, though. It is designed to be a quick way to check what your API returns (or even an external API). I would imagine integrating it into unit testing would be a lower priority compared to some of the other features it is still missing.
Could you put an example if the API used token authentication?
Exactly what I also thought about !
Was about to post the same. This video is a good "Getting started" video, but once you add auth (and you almost always do), it seems to get a lot trickier...
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
I'm assuming it would be similar to using curl right? the way you do it in postman but the curl code, it probably works here too, getting token and storing it in a variable etc. But will have to try.
@ghost mall I still haven’t found a good way to obtain this token. Usually tokens are obtained from a separate auth api and expires within hours. This doesn’t rhyme well on testing. It would be nice to see an example on how to mock a token, if this is at all possible 🤓.
I followed the Microsoft tutorial on this but my Visual Studio just tells me the request was completed but there were errors and never pops up with a window showing anything about the response. Any ideas on how to fix this?
Read the error messages and try to track down what they are telling you.
thank you
You are welcome.
What would happen if you do a PUT call, but the person is not in the entries. And what will happen if you do a DELETE call, but the person is also not in the entries?
You would see the error result returned in the execution window, just like an end user would if they made those calls.
wondering how to add bearer tokens and other auth dependencies
That is coming with time. They are slowly adding pieces to it. They need to pull the authentication information from a secret location. That way you aren't checking credentials into source control.
It looks like it's far further developed for VSCode that VS and may always be. I havent read too much yet but an essential requirement for me is to be able to pass retrieved auth tokens between calls.
That is coming, but it takes a bit. They are just developing this feature now. It will be a part of .NET 8, which releases in November 2023. I don't know if they will have authentication fully handled yet, but they are working carefully towards it. The trick is that you need to pull data from a secret location in order to test authentication. Otherwise, you would be checking credentials into source control.
How would you send a bearer token using http docs? I'm not having much luck with that..
Oops.. figured it out. I have a website that calls the api, and I followed the instructions too closely. I put the url of localhost instead of the url of my api I'm calling. So I created a token variable with the token value under my root url, then I added that header like this: Authorization: Bearer {{token}} . Worked great!!
is this feature applicable in visual studio code sir? thanks
Changes to Visual Studio don't affect VS Code because they are two different systems. VS Code can work with http files, though.
Very Cool Tim.😊👍 But how we can test endpoints with passing a jwt token to authenticate and authorize someone if they have access to retrieve data from an action method ? Can this be done in http files if so please cover that in another video
Not automatically yet. They are working on it, though. They need to pull authentication data from a secret location, though, so you aren't checking credentials into source control.
@@IAmTimCorey Thank you 😍😍
Whats the benefit over using Postman?
It is checked into source control, it is sharable among the team, it is a quick way to run a test of the API, it is simple, etc. There is still definitely a place for Postman, though.
@@IAmTimCorey It's a cool feature but if Postman is already being used to test in non-local environments, it feels a little like I'm having to repeat myself having to define that test twice.
Hi tim
When the monthly pass will be open
It will be a while. It was open recently. You can email help@iamtimcorey.com to see if you can get access early. No promises, though.
but ur not really unit testing they need bring asserts in and this woulr be brilliant
That's not what we are doing here. Unit testing is done in a unit testing project. This is for testing what you are building as you build it or verifying what data gets returned, etc. If you did want to call it a test type it would be an end-to-end test, since this actually runs the full process.
That feature is near useless.
Why?