EP 49: Use .http files in Visual Studio to test APIs

Muhammad Waseem
Weekly .NET Newsletter
2 min readApr 2, 2024

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 {}

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 2 ways I can help you:

  1. Promote yourself to 9500+ subscribers by Sponsoring my Newsletter
  2. Previous sponsors : Jetbrains, Workflow Engine
  3. Become a Patron and get access to 200+ .NET Questions and Answers

Do not lose hope, nor be sad — Quran

--

--