.HTTP Files in Visual Studio 2022

As of Release 17.5, Visual Studio 2022 now has support for declaring and running http requests from within the IDE. Similar to such tools as Postman, you can define requests, VERBS, headers, and request bodies. Then run them against your local development environment or any public services.

To read more about this new feature, please read the release blog for Visual Studio 2022.

The best part is that you can create these .http/.rest files in your project with all of your other source files, so you can check them into your repo with your source code. This makes it very convenient to keep some easy test files around and up-to-date with your code changes.

Just create a new text file and give it a .http file extension (my-queries.http). Then, place your queries in the file:

@port = 7019
@baseUrl = https://localhost:{{port}}
@contentType = application/json
@id = TST

### Get all countries
GET {{baseUrl}}/api/v1/countries

### Create new country
POST {{baseUrl}}/api/v1/countries
Content-Type: {{contentType}}

  "name": "Test",
  "officialName": "TestLandia",
  "alpha2Code": "TS",
  "alpha3Code": "TST",
  "numericCode": "042"

### Update country with id
PUT {{baseUrl}}/api/v1/countries/{{id}}
Content-Type: {{contentType}}

  "name": "Test2",
  "officialName": "TestLandia2",
  "alpha2Code": "TS",
  "alpha3Code": "TST",
  "numericCode": "042222"

### Get country with specified id
GET {{baseUrl}}/api/v1/countries/{{id}}

### Delete country with specified id
DELETE {{baseUrl}}/api/v1/countries/{{id}}

Then, run the individual queries to get the API result (this example runs against a localhost service):

Fig 1 – HTTP Query with response side-by-side

The response and header from your API shows in the split window on the right. You can see all of the response information, including the response body as formatted JSON.

Visual Studio Code had a similar extension for a few years, which was very handy as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s