In this post, we’ll see what a REST Client for Visual Studio 2022 is and what we can do with it.
The REST Client is an extension made by Mads Kristensen and is inspired by the Visual Studio Code extension Rest client created by Huachao Mao. So, it mimics similar functionality.
In my previous post on Minimal APIs, I’ve come across this REST client in Visual Studio 2022 and found really useful when testing minimal APIs.
REST client requires the file to have an .http
extension to detect the operations to invoke.
A basic GET request can be something like this.
GET https://google.com
Once we wrote the request we can either click on the little green right arrow on the request line or just right-click on the request line and select Send Request
option from the context menu or Ctrl
+ Alt
+ S
to make a request.
And the response to the request will be displayed on the right panel.
We can compose multiple requests by having a separator (#) character between the requests. The # character marks is used to mark the end of the request and also can be used for comments.
If we have to make a bunch of requests, then we’d have to separate the requests by inserting a hash character (#) between the requests or just do double new lines.
This is how it looks like if we don’t give provide a # character or double new lines.
Notice we don’t see the green arrow icon next to coderethinked.com GET request and if we make a request on the second request we’d still end up sending a request to google.com instead of coderethinked.com.
So, having double new lines or inserting a # (hash) character between the requests will make them visible to the plugin (yeah, it looks like a bug).
With the custom variables feature, we can re-use the hostname, port, content types, auth token, etc for every request. This is a good feature for every REST client.
So, we can have requests like these
# VARIABLES @hostname = localhost @port = 5143 @host = http://{{hostname}}:{{port}} @contentType = application/json ## Get a user by Id GET {{host}}/user/1 ## update PUT {{host}}/user/update/1 Content-Type: {{contentType}} { "userName": "kchintala", "firstName": "Karthik", "lastName": "Chintala" } ## after update, Get a user by Id 1 GET {{host}}/user/1 ### delete user with id 1 DELETE {{host}}/user/1 ## after deleting user with id 1, try to fetch user with id 1 GET {{host}}/user/1
Observe how we composed the PUT request in the above snippet. We set the Content-Type
and also pass the JSON to update the user with id 1. Likewise, we can also set up Authentication
tokens and other meta information as we need.
Notice how the reusable variables are consumed in all the requests with mustache syntax.
Here’s the demo of running through all the requests in .http
file.
Karthik is a passionate Full Stack developer working primarily on .NET Core, microservices, distributed systems, VUE and JavaScript. He also loves NBA basketball so you might find some NBA examples in his posts and he owns this blog.
In this post, we’ll see how to test gRPC Server applications using different clients. And… Read More
In this post, we'll create a new gRPC project in ASP.NET Core and see what's… Read More
In this blog post, we’ll see how to run dotnet core projects without opening visual… Read More
Programmatically evaluating policies is useful when we want to provide access or hide some data… Read More
We saw how we could set up policy-based authorization in our previous article. In this… Read More
What is policy-based authorization and how to set up policy-based authorization with handlers and policies… Read More
This website uses cookies.