Built In Rest Client in .NET Core

Introduction

.http files are simple text files in which HTTP requests and answers can be specified. These files are used to test, describe, and distribute APIs.

.http files contain a variety of features used to specify API queries and answers. Lines within the file may contain information such as the HTTP method (GET, POST, PUT, etc.), URL, headers, parameters, and body.

The Visual Studio 2022.http file editor allows you to test Web APIs directly in Visual Studio, without having to go to another tool. In this article, I demonstrated how to send HTTP requests with .http files.

The .http file editor in Visual Studio has a user interface for sending HTTP requests and viewing request/response data. This feature is available in Visual Studio 2022, version 17.6 and later.

To demonstrate, I established an ASP.NET Core Web API project in Visual Studio 2022. When you create a new project, you may already see the .http file in the solution.

Previously we used different mechanisms for testing the API on local machines, like.

  1. Postman
  2. Swagger
  3. Unit Testing

How to create the .http file in the application?

  1. Right-click on the project
  2. Click on the Add button and Click on the New Item
  3. Search the http and it will show the HTTP File.
    HTTP File
  4. Change the name to whatever you want and click on the Add button.
    Add button

Syntax for .http file

  • Comments
  • Variables
  • Requests
  • Request Headers
  • Body

If you want to create the variable in a .http file, then use the @ sign to declare the variables.

// declare the variable
@baseURL = https://localhost:7051

The variables can be referenced in requests by enclosing their names in double curly brackets (see the line with the GET request).

// use the variable
GET {{baseURL}}/api/Values

Lines beginning with # or // are comments, and VS ignores them when submitting HTTP requests.

# declare the variable
@baseURL = https://localhost:7051

// use the variable
GET {{baseURL}}/api/Values

A file can contain numerous requests by separating them by ###.

GET {{baseURL}}/api/Values/a210acdd-8edb-44d3-a4c0-f23de215c450
###

// use the variable
GET {{baseURL}}/api/Values

As an illustration, let’s run the application and test the APIs.

Once the application is run, you will need to open the .http file, and you will see the below type of content.

Send request

Click on the Send request button.

The Formatted

 Formatted

The Raw

 Raw

The Headers

 Headers

The Request

Request

While testing the APIs, if you want to add a debug point, then it's also possible. Just add the debug point where you want, and after sending the request, you will see the debug hit as well.

APIs

Request Headers

It is possible to include one or more headers with the request. You can include the header in the line below the request line (no blank lines), using the format HeaderName: Value. For illustration.

GET {{baseURL}}/api/Values/{{id}}
X-API-Subscription: secret value

Request Body

To include a request body, place a blank line after the request (or after the header if you have one). For illustration

POST {{baseURL}}/api/Values
Content-Type: application/json

{
    "id": "123",
    "firstName": "Jaimin",
    "lastName": "Shethiya",
    "mno": 7990233523
}

Request Body

Here is the full .http file content.

# declare the variable
@baseURL = https://localhost:7051
@id = a210acdd-8edb-44d3-a4c0-f23de215c450
@deleteId = 6bb4aab9-faf3-44ee-af75-d08ff2cac5c7

###
GET {{baseURL}}/api/Values/{{id}}

###
// use the variable 
GET {{baseURL}}/api/Values

###
POST {{baseURL}}/api/Values
Content-Type: application/json

{
    "id": "123",
    "firstName": "Jaimin",
    "lastName": "Shethiya",
    "mno": 7990233523
}

###
PUT {{baseURL}}/api/Values/1234
Content-Type: application/json

{
    "id": "a210acdd-8edb-44d3-a4c0-f23de215c450",
    "firstName": "Jaimin",
    "lastName": "Shethiya",
    "mno": 7990233523
}

###
DELETE {{baseURL}}/api/Values/{{deleteId}}

Supported features

  • Simplicity
  • Git support
  • Independence
  • Integration
  • Quick tests

Unsupported features

  • Optional HTTP Method
  • Request History
  • Dynamic Variables
  • GraphQL requests
  • cURL request
  • Certificate-based authentication
  • System variables
  • Save the response body to the file

Conclusion

The Visual Studio 2022 .http file editor allows you to submit HTTP queries to your Web API endpoints without leaving the Visual Studio environment, making it faster to test your endpoints without the need for Swagger, Postman, or other tools.

All the tools have some different unique features, so based on your daily use case, you wanted to decide which one was useful to our application and based on that work.

We learned the new technique and evolved together.

Happy coding. :)