REST Client For Visual Studio 2022
Tags: tools, visualstudio, httpextension, restclient
Published: 2022-08-31 04:09:05

---
title: REST Client For Visual Studio 2022
published: true
date: 2022-08-02 14:37:00 UTC
tags: Tools,VisualStudio,httpextension,restclient
canonical_url: https://coderethinked.com/rest-client-for-visual-studio-2022/
---

In this post, we’ll see what a REST Client for Visual Studio 2022 is and what we can do with it.

The REST Client is an extension made by Mads Kristensen and is inspired by the Visual Studio Code extension [Rest client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) created by Huachao Mao. So, it mimics similar functionality.

In my previous post on [Minimal APIs](https://dev.to/karthikchintala/what-are-minimal-apis-in-aspnet-core-6-184k-temp-slug-8339504), I’ve come across this REST client in Visual Studio 2022 and found really useful when testing minimal APIs.

## The .http file

REST client requires the file to have an `.http` extension to detect the operations to invoke.

A basic GET request can be something like this.

```
GET https://google.com
```

![Demo of REST Client for Visual Studio 2022 making a GET request](https://coderethinked.com/wp-content/uploads/2022/08/image-1024x401.png)
_Figure 1: Demo of REST Client for Visual Studio 2022 making a GET request_

Once we wrote the request we can either click on the little green right arrow on the request line or just right-click on the request line and select `Send Request` option from the context menu or `Ctrl` + `Alt` + `S` to make a request.

And the response to the request will be displayed on the right panel.

## Composing multiple requests

We can compose multiple requests by having a separator (#) character between the requests. The # character marks is used to mark the end of the request and also can be used for comments.

If we have to make a bunch of requests, then we’d have to separate the requests by inserting a hash character (#) between the requests or just do double new lines.

This is how it looks like if we don’t give provide a # character or double new lines.

![REST client for Visual Studio 2022 not showing multiple requests without # character or new lines](https://coderethinked.com/wp-content/uploads/2022/08/image-1.png)
_Figure 2: REST client not showing multiple requests without # character or new lines_

Notice we don’t see the green arrow icon next to coderethinked.com GET request and if we make a request on the second request we’d still end up sending a request to google.com instead of coderethinked.com.

So, having double new lines or inserting a # (hash) character between the requests will make them visible to the plugin (yeah, it looks like a bug).

![REST client showing multiple requests with # character between the requests](https://coderethinked.com/wp-content/uploads/2022/08/image-2.png)
_Figure 3: REST client detecting multiple requests with # character between the requests_

## Custom variables

With the custom variables feature, we can re-use the hostname, port, content types, auth token, etc for every request. This is a good feature for every REST client.

So, we can have requests like these

```
# VARIABLES
@hostname = localhost
@port = 5143
@host = http://{{hostname}}:{{port}}
@contentType = application/json

## Get a user by Id
GET {{host}}/user/1

## update
PUT {{host}}/user/update/1
Content-Type: {{contentType}}

{
	"userName": "kchintala",
	"firstName": "Karthik",
	"lastName": "Chintala"
}

## after update, Get a user by Id 1
GET {{host}}/user/1

### delete user with id 1
DELETE {{host}}/user/1

## after deleting user with id 1, try to fetch user with id 1
GET {{host}}/user/1

```

Observe how we composed the PUT request in the above snippet. We set the `Content-Type` and also pass the JSON to update the user with id 1. Likewise, we can also set up `Authentication` tokens and other meta information as we need.

Notice how the reusable variables are consumed in all the requests with mustache syntax.

Here’s the demo of running through all the requests in `.http` file.

![REST CLIENT for Visual Studio 2022 DEMO](https://coderethinked.com/wp-content/uploads/2022/08/restclientdemo.gif)
_Figure 4: REST CLIENT for Visual Studio 2022 DEMO_

## Notable Features in REST client for Visual Studio 2022

- **URL validation enabled** : If the URL or the parameters are wrong we will not be able to make a request.
- **Adjusting the request timeout** : The request timeout can be adjusted in the REST client settings within visual studio 2022.

![Enable validation and adjusting request timeout in REST client options in REST Client for Visual Studio 2022](https://coderethinked.com/wp-content/uploads/2022/08/image-4.png)
_Figure 5: Enable validation and adjust request timeout in REST client options_

- Can do **all CRUD operations**
- **Custom variables** (reusability) and using them with mustache syntax
- **Output in another results panel** : For every request, the output window (right panel) will be refreshed with the new response. It would’ve been better if we could have multiple tabs for each response. 
- Can **cancel the request** : Once we make a request, we should see a green hourglass icon next to the request. Clicking the hourglass will rescind the request.

![Can cancel request by clicking on the hour glass on the right in REST Client for Visual Studio 2022](https://coderethinked.com/wp-content/uploads/2022/08/image-3.png)
_Figure 6: Can cancel your request by clicking on the hourglass on the right_

## References

1. [REST Client for Visual Studio 2022](https://marketplace.visualstudio.com/items?itemName=MadsKristensen.RestClient)
2. [REST Client for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=humao.rest-client)

The post [REST Client For Visual Studio 2022](https://coderethinked.com/rest-client-for-visual-studio-2022/) appeared first on [Code Rethinked](https://coderethinked.com).