Visual Studio Code as an Advanced REST Client

Development
  • Marcin Golonka
  • 03-01-2023

Visual Studio Code as a REST Client

Introduction

VS Code quickly established itself in the IT market as a comprehensive programming environment for practically every technology. Above all, Microsoft made it available for free, disrupting the hobbyist market that didn’t need a paid environment. Additionally, the open-source nature led to the community enthusiastically enhancing Code with an open extension interface.

In my daily work, VS Code primarily serves as a quick programming notepad with syntax highlighting and a set of tools useful in everyday tasks. Today, I’d like to showcase the latter aspect. What if VS Code replaced our dedicated tools for REST interfaces?

Extension - REST Client

Chinese developer Huachao Mao published the REST Client extension in the marketplace in 2016, and it quickly climbed the ranks of the most popular VS Code add-ons. Currently, it is, in my opinion, one of the more interesting extensions that have appeared in the Marketplace.

Installation

To install the plugin, simply search for it in the Extensions tab and click install.

1

Basic Usage

To start using the REST Client, we need to create a file with the extension .http or .rest and invoke a sample endpoint using the RFC 2616 protocol syntax.

2

After clicking the “Send Request” button, we’ll receive a response from the server.

If we want to include more than one HTTP request in a single document, we can separate them with a triple hash (###).

More Advanced Examples

Multiple Requests and Variables

Variables are defined using @. We can refer to:

  • Locally defined variables in the file
  • Environment variables, making it easy to switch, for example, URLs
  • Predefined random variables, for example, {{$randomInt min max}}
  • System variables, for example, DOTNET_ENVIRONMENT

If we want to make a POST request and create a resource, we only need to specify the appropriate HTTP headers and the request body. We can also load the body from a file.

3

Export to cURL Command

Another feature worth knowing is the ability to generate cURL requests for our queries. Simply run the Command Palette (default F1) and execute the command Rest Client: Copy Request As cURL.

4

This way, we can use cURL in the command line.

GraphQL Support

Since GraphQL is also transported via the HTTP protocol, we can query graph APIs using VSCode.

5

Code Snippet Generation

The extension also allows generating code snippets for the endpoints in the file. The supported list of languages is extensive, including Java, C#, JavaScript, Python, etc.

6

But Why All This?

In my opinion, the ability to create files with REST requests opened a gateway to keep a basic test set for endpoints in the repository, eliminating the need to run another tool.

Moreover, since they are formally test files, it is relatively easy to version your or team’s handy set of queries.