testing http requests from visual studio code

One of my favorite tools is an extension of Visual Studio Code to make HTTP Calls: REST Client by Huacho Mao https://marketplace.visualstudio.com/items?itemName=humao.rest-client

How does it work?

First install the Visual Studio Code extension, in Visual Studio Code on the left toolbar click Extensions (control+shift+x) and search/install ‘REST Client’ by Huachao Mao.

image

Now create a new text file with extension .http and add the following code:

## GET Request
GET https://jsonplaceholder.typicode.com/posts

### POST Request
POST https://jsonplaceholder.typicode.com/posts
Content-Type: application/json

{
"userId" : 1,
"title": "Hello World from REST Client VSCode",
"body": "This is how to make a REST Call from inside VSCode"
}

In this text file there are two requests, one HTTP GET for retrieving items, one HTTP POST for creating items.

When loaded in Visual Studio Code, it looks like:

image

Above the 2 requests there is a button added ‘Send Request’. When this button is clicked the HTTP request is send and the HTTP response headers/body will show up in another tab:

image

In a more advanced scenario we can inspect the response and fill variables . In the following example a login ‘token’ variable is used after HTTP POST Login:

# @name login
POST http://myserver/api/Login
Content-Type: application/json

{
  "username": "user",
  "password": "password"
}

###
@token = {{login.response.body.token}}

### Get more info

GET http://myserver/api/Things
token: {{token}}

This simple file can be checked into the source control system and will become useful when testing API’s.

Leave a comment