P M A J E W S K I

Please Wait For Loading

Effortless API Testing with Visual Studio Code: Quick and Easy Guide - Software Developer's Tour

    You Are Currently Here!
  • Home
  • ProgrammingToolsEffortless API Testing with Visual Studio Code: Quick and Easy Guide
Rest client

Effortless API Testing with Visual Studio Code: Quick and Easy Guide

Have you ever wondered how to test your API endpoints? There are many options to do so. You can use:

Swagger

Postman

Insomnia

(other less known software)

or our star for today…

Visual Studio Code

Visual Studio Code (VSC) is a really powerful tool. Calling VSC an IDE wouldn’t be entirely accurate, as its functionality largely depends on extensions.

One time VSC can be typical text editor, and the other time you can use VSC to manage your Linux server files over SSH using https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh.

Firstly, let’s create a new Web API project and modify the default WeatherForecastController with the code below.

using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;

namespace TestApi.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    [HttpGet("get")]
    public IActionResult Get()
    {
        return Ok(Enumerable.Range(1, 5));
    }

    [HttpGet("get-protected")]
    public IActionResult GetProtected()
    {
        if (!Request.Headers.ContainsKey(HeaderNames.Authorization) || Request.Headers[HeaderNames.Authorization] != "Bearer testKey")
            return Unauthorized();

        return Ok(Enumerable.Range(1, 5));
    }

    [HttpPost("post")]
    public IActionResult Post([FromBody] TestRequest request)
    {
        return Ok(request);
    }

    public class TestRequest
    {
        public required string Name { get; set; }
        public int Age { get; set; }
    }
}

As you can see, there are three endpoints that we will test today:

Get

GetProtected

and Post

These three endpoints will cover all our base cases, so let’s get started!

Install REST Client extension

Rest client installed

Create file with .http extension

First sending of the request

request

The arrow points to the button which sends the request, and on the right panel, you can see the response from the server.

GET https://localhost:7081/WeatherForecast/get

Let's make authorized request

unauthorized request

We’ve received a 401 Unauthorized error. I’ve created a fake authentication based on a Bearer token. If you are not familiar with Bearer tokens, read more https://docs.github.com/en/rest/authentication/authenticating-to-the-rest-api?apiVersion=2022-11-28

What should we do? Just add an Authorization header

Auth header request
GET https://localhost:7081/WeatherForecast/get-protected
Authorization: Bearer testKey

How about POST http method?

Nothing easier!

What is worth highlighting is that VSC automatically suggests possible HTTP MIME types.

http mimes

The full request

POST request
POST https://localhost:7081/WeatherForecast/post
Content-Type: application/json

{
    "name": "pawelmajewski.com",
    "Age": 22
}

Improvements

We’ve got this. Our tests work perfectly, but… what if the base URL changes?

Variables

Yes!We will replace repetitive elements with variables.

file after refactor
@baseUrl = https://localhost:7081
@apiToken = Bearer testKey
@contentTypeApplicationJson = application/json

GET {{baseUrl}}/WeatherForecast/get

###

GET {{baseUrl}}/WeatherForecast/get-protected
Authorization: {{apiToken}}

###

POST {{baseUrl}}/WeatherForecast/post
Content-Type: {{contentTypeApplicationJson}}

{
    "name": "pawelmajewski.com",
    "Age": 22
}

Now, I’m happy with this code. As you can see, VSC is a very powerful tool with an enormous number of extensions that make VSC a multitool.

Additionally, what makes this tool very simple and fast to use for programmers is that here you simply write code, without needing to learn a new UI to do anything.

leave a comment