DEV Community

Cover image for API Testing using .http Files in .NET
Ahmed Elmehalawi
Ahmed Elmehalawi

Posted on

API Testing using .http Files in .NET

In .NET 8, a new type of file, the .http file, has been introduced in API projects. The purpose of this file is to simplify API testing directly by writing and using simple HTTP commands.

The .http file is a plain text file that contains HTTP commands such as GET, POST, PUT, DELETE, and other requests used in APIs. Instead of relying on external tools like Postman or cURL, you can write your requests inside this file and run them directly from within Visual Studio or VS Code.
The main goal is to provide a simple and fast environment to test the APIs you're working on. Instead of switching to external tools for sending requests, you can handle everything inside the IDE. This also helps dev teams share a set of ready-made requests for testing purposes.

The file is quite simple and contains multiple requests in a specific format.
For example:

GET https://localhost:5000/api/products
###
POST https://localhost:5000/api/products
Content-Type: application/json
{
 "name": "Product X",
 "price": 100
}
Enter fullscreen mode Exit fullscreen mode

One of the great features of .http files is that you can select which environment to run them in. For example, you might have an API running in different environments like Development and Production.

You can create an .env file that contains different settings for each environment:

{
 "dev": {
 "HostAddress": "https://localhost:5000"
 },
 "remote": {
 "HostAddress": "https://api.app.com"
 }
}
Enter fullscreen mode Exit fullscreen mode

Then, use these variables inside the .http file:
GET {{HostAddress}}/api/products

Top comments (0)