VS Code as a Testing Client For Web API

This article will discuss Testing Client for RESTful Web API. Specifically, concentrate on using VS Code to Test Web API.

Introduction

This is the structure of this article,

  • Introduction
  • Regular Tool for Testing Web API
    • Fiddler
    • Postman
    • Swagger
  • Alternative Tool - VS Code
  • Summary

A - Web API, and the role in Microservice

In recent years, say, ten years, the Microservice Platform became popular and more popular.  The reason behind this is that the Microservice decomposes the application into discrete services that implement specific business functions, often referred to as “loosely coupled,” can then be built, deployed, and scaled independently. It is the trend of software development.

Typical Microservice includes two parts: Client and API. The client is the consumer that could be applications of a smartphone, tablet, Windows, Mac, or any kind of browser. The API (Application Programming Interface) is the service provider that could be accessed by any or all of the kinds of Clients, usually indicating the RESTful service or JSON format output.

Theoretically, API could be tested by any type of its clients, whatever, console application, Web application or smartphone, and so on. However, during the development process, using a simulated tool as a client will make the development much easier. 

The popular tools we used to test Web API, such as,

However, in some situation, for example, some company has a very strict security policy, this kind of tool is blocked. We have another choice is to use Swagger that is a default Web API testing tool built in Microsoft .NET Core Web API development (for .Net Framework, if you want to use Swagger, you have to add it by yourself). For using Swagger, one might see my previous article, ASP.NET Core, Web API - Entity Framework Call Stored Procedure, However, the shortcoming for Swagger is it is an application built-in testing tool for Web API. 

So, an alternative tool for Fiddler, Postman, or Swagger for testing Web API is necessary for some special situations. One of my colleagues suggested that VS Code with REST Client extension can do the job. This is what I will introduce in the next section.

B - Testing RESTful Web API by VS Code

These two links are what I got from my colleague, those are enough for our state to use VS Code as a testing client for Web API,

B - 1: Install REST Client 

In VS Code environment:  Click Manage (Left Bottom Corner) => Extensions (or Ctrl+Shift+X).

 

In Extensions: Marketplace: type in 'Rest' to search, you'll see the extensions: REST Client.

You will get REST Client, and install,

B - 2: Open a file to Use REST Client Testing Web API 

After installing REST Client extension, the next step will be very straightforward,

1, Open a file and use it to test Web API

The file could be any kind of file, such as a .txt file, or somebody suggested as a .http file, but anyway, it does not matter at all. I open a file without saved, and without any name for it, like this,

Type in the link for the Web API you want to test. Now we are ready to run the REST Client command to test Web API.

2, Open REST Client Extension Command prompt:

Press Function 1 Key in keyboard,

Or, use short cut: Ctrl + Shift + P,

We will get the command line prompt for VS Code on the top screen, then type 'rest' in the search field, we got the REST Client related commands such like,

Highlight the link in the file you opened, and run the REST Client command: Send Request, you will get a response from another screen like this,

If you have two or more Web API links in the file, highlight the one you want to test, then press the REST Client Send Request command, the related result will be shown on the next screen.

B - 3: More Complex Cases

For testing Web API, we have four verbs,

  • Get --- for READ
  • Post --- for CREATE
  • Put --- for UPDATE
  • Delete --- for DELETE

1, Get

Actually, when we run the REST Client Send Request command with Web API link, by default, it will be Get, say, if we add a verb Get before the link, and run, the result will be the same as before:

2, More Complex Cases

For the complex cases, such as Post, Put, Delete, we can get help and examples from the REST Client Extension document,

For Post, we have,

However, it is NOT as convenient as other Web API tools, such as Postman, Fiddler, or even Swagger. For example, for Swagger, if we run the POST method, we will get its schema automatically like this,

i.e., we do not need to read documents when we use Swagger, it is just as user-friendly as what you can get from the screen.

Finally, I tried to make a successful run for POST, but could not.

In this case, I have other tools to do my job, I will not devote too much time to it, and I will leave it as a practice to the article reader.

Summary

As an alternative, VS Code could be a testing tool for RESTful Web API through the REST Client Extension. It is very easy to use in the case of GET verb, however, for other verbs, you probably need to read the documents to get the syntax to complete your job.

Reference


Similar Articles