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.
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!
Naming conventionIt's a good practice to name your files with words that clearly describe their purpose. Today, I'll test the endpoints in WeatherForecastController, so I've named the file WeatherForecastControllerTests.http.
First sending of the request
The arrow points to the button which sends the request, and on the right panel, you can see the response from the server.
What should we do? Just add an Authorization header
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.
The full 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.
@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.
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.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
https://marketplace.visualstudio.com/items?itemName=humao.rest-client
Create file with .http extension
First sending of the request
The arrow points to the button which sends the request, and on the right panel, you can see the response from the server.
Let's make authorized 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
headerHow about POST http method?
Nothing easier!
What is worth highlighting is that VSC automatically suggests possible HTTP MIME types.
The full request
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.
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.
Archives
Understanding K-Nearest-Neighbors (KNN) – Essential Machine Learning Algorithm
2024-10-06A Step-by-Step Guide to Web Scraping for Beginners
2024-07-12Categories
Meta