Use .http files in Visual Studio
How do you test your APIs in .NET , answer could be
-
Postman
-
Swagger
-
Unit testing
What if I tell you we have new way to test our APIs
.NET 8 Web API with default .http file
Using .http file we can test our .NET APIs, although this option was available there since a long time, but I noticed it when I have recently created a .NET 8 API .
It is by default created with .NET 8, it looks like this :
What is the purpose of this file
As per Microsoft docs .http file editor provides a convenient way to test ASP.NET Core projects, especially API apps. The editor provides a UI that:
-
Creates and updates .http files.
-
Sends HTTP requests specified in .http files.
-
Displays the responses
How does this file looks like?
By default this file contains this code :
We can add new variable by using at the rate (@) sign and use it.
Three hash tags (###) are used as delimiter to tell this file that previous API has ended here and we have other API after that.
How can we send the request ?
Run your project and then click on Send request and on right side you would see the results of your request.
On similar fashion we can add other requests for other HTTP Methods
-
OPTIONS
-
GET
-
HEAD
-
POST
-
PUT
-
PATCH
-
DELETE
-
TRACE
-
CONNECT
How to add headers and body ?
We can add header in key : value style. And to add body we can use curly brackets {}
POST https://localhost:5256/weatherforecast
Content-Type: application/json
{
"date": "2023-05-10",
"temperatureC": 30,
"summary": "Warm"
}
To learn more about how to set environment files, user secrets and azure key vault check out Microsoft Docs.
Whenever you're ready, there are 3 ways I can help you:
- Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
- Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
- Patreon community : Get access to all of my blogs and articles at one place