REST Client – great VSC extension for HTTP requests

Last Wednesday, together with Piotr Gankiewicz, I started series of technical talks about microservices, called „Distributed .NET Core”. The presentation is mainly focused on live demos, each of which covers a different area of the architecture. Because the application we wrote is a web application, each demo started with HTTP call to the appropriate REST API endpoint… and that caused a little issue (at least for me) because each call has different:

  • URL
  • method type
  • body (for POST and PUT)
  • headers (some require Authorization)

Because I was in the middle of the presentation I didn’t want to type each request in Postman from scratch (especially those with complex JSON bodies) because I hadn’t a time for that, that would be boring as hell and I’d screw that up for sure.
Therefore, what alternatives I had?

  • type everything in the notepad and paste it to the Postman (not very clever solution and doesn’t look PRO)
  • make all calls before the talk and use Postman history (I chose that one but it turned out that during the talk I had to change the Authorization header in each request because access token has expired…)
  • use Postman’s collections (quite handy but I couldn’t find a way to create a variable for parameters, headers etc.)

All the above options solve my problem better or worse, but none of them has a very important characteristic for me. You see, I like keeping everything related to the project in the repository, no matter if it’s build pipeline (Cake), CI file or something else. If something can be an integral part of the project – it’s better for me. That’s why I started looking for some tools which would allow me keeping all my HTTP requests in a file and use it quickly in my Visual Studio Code. I find one, it’s called REST Client.

 

REST Client

REST Client is an extension for Visual Studio Code which does exactly what I wanted. It allows you to create dedicated files for your HTTP request and send them directly from code editor without a need to use additional tools like Postman. What’s worth mentioning is that created files can be easily parametrized and organized. Let’s give it a try.

The extension can be found here. Having it, we can create a file for our HTTP request. REST client support files with two custom extensions: *.http and *.rest. I’ll stick to the first one like so:

 

 

For simplicity sake of this article, I put the file into default ASP.NET Core app which contains ValuesController (a little bit modified):

 


    public class Model
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        [HttpPost]
        public string Post([FromBody] Model model)
            => $"Model created by {model.FirstName} {model.LastName}";

        [HttpPut("{id}")]
        public string Put(int id, [FromBody] Model model)
            => $"Model with id={id} updated by {model.FirstName} {model.LastName}";

        [HttpDelete("{id}")]
        public string Delete(int id)
            => $"{id} deleted";
    }

 

The ValuesController.http file looks as follows:

 

### Get all values
GET http://localhost:5000/api/values

### Get value by id
GET http://localhost:5000/api/values/1

### Create model
POST http://localhost:5000/api/values
content-type: application/json

{
    "firstName": "John",
    "lastName": "Brown"
}

### Update model
PUT http://localhost:5000/api/values/1
content-type: application/json

{
    "firstName": "David",
    "lastName": "Smith"
}

### Delete value
DELETE http://localhost:5000/api/values/1

 

Believe or not that’s all you need to test our API. The question is how to test each request? Well, when you’ll paste it into VSC, you’ll notice „Send Request” link above each one:

 

 

Let’s click it and see what will happen (remember to have your application up and running):

 

 

 

Simple is that! No need to use any other tools, you can create dedicated files for each controller, and the response pops in the same window. Nice.
As I wrote earlier, created files can be also easily parametrized using variables. Let’s see the example:

 

@id = 1
@token = Bearer MY_CUSTOM_TOKEN

### Get all values
GET http://localhost:5000/api/values
authorization: {{token}}

### Get value by id
GET http://localhost:5000/api/values/{{id}}
authorization: {{token}}


### Create model
POST http://localhost:5000/api/values
authorization: {{token}}
content-type: application/json

{
    "firstName": "John",
    "lastName": "Brown"
}

### Update model
PUT http://localhost:5000/api/values/1
authorization: {{token}}
content-type: application/json

{
    "firstName": "David",
    "lastName": "Smith"
}

### Delete value
DELETE http://localhost:5000/api/values/{{id}}
authorization: {{token}}


 

 

Thanks to that, if you’ll have to do some global change like setting new access token in the Authorization header, you can do this once instead of doing this each time you send the request (like I did :D).

Of course, REST Client offers way more, so you should definitely check it on the official website. For me, this extension is truly awesome and I’ll definitely use it during future tech talks. Easy to use, integrated with VSC, easily shareable with a source code – ideal.

You may also like...