Friday, April 12, 2024
HomeC#Calling Net APIs utilizing the dotnet CLI and HTTP Recordsdata with Visible...

Calling Net APIs utilizing the dotnet CLI and HTTP Recordsdata with Visible Studio –

Visible Studio 2022 17.5 consists of new HTTP shopper tooling. This device makes it simple to create API calls immediately from Visible Studio, with an incredible output of the API outcomes. It’s simple to immediately debug API invocations with this device. One other device which I’m utilizing since some years is the HTTP REPL which is obtainable as a dotnet device. This text exhibits how each of those instruments.


A device I’m already utilizing some years is microsoft.dotnet-httprepl that’s out there as a .NET device. To put in it globally for the present person, you should utilize dotnet device set up microsoft.dotnet-httprepl -g.

When this device is put in, you should utilize the httprepl command to connect with an API when OpenAPI is obtainable, listing all of the APIs out there, and do HTTP requests. Let’s get right into a pattern. Invoking httprepl with out passing a connection, this device is began disconnected.

To see all of the out there instructions, sort assist. To connect with an API, use the join command. The next command connects to the API at https://localhost:7261. You get the data if OpenAOI description was discovered. If so, you may stroll by way of the completely different APIs and use the ui command to open the OpenAPI UI web page.

dotnet httprepl - Connect

The OpenAPI (Swagger) UI used with the pattern software is proven with the subsequent picture.

OpenAPI (Swagger) UI

With the ls command, you may listing all of the APIs out there. You may stroll by way of the APIs utilizing the cd command. The ls command exhibits the enpoints out there with the present path. Utilizing the recursive (-r) possibility, you may see all of the endpoints out there:

dotnet httprepl - list

Let’s ship some requests. With the get command, you may ship a GET request to the API. The primary get request to the /video games API returns an empty array as a result of no recreation was created but. Utilizing the put up command to ship a POST request creates a brand new recreation. To go JSON infomration with the physique of the POST request, I’m submiting the content material of the file creategame.json utilizing the -f possibility. When you don’t provide the filename, an editor is opened the place you may fill out the content material of the physique. You may configure the editor to make use of with the pref (preferences) command.

dotnet httprepl - get and post

Visible Studio Built-in HTTP Consumer

With Visible Studio 2022 17.5, a brand new HTTP shopper device is obtainable. This device is built-in into Visible Studio and makes it simple to create API calls immediately from Visible Studio.

To make use of this device, create a textual content file with the file extension .http. An merchandise template HTTP File is obtainable:

HTTP File Item Template

Inside this file, you may create variables prefixed with @ akin to @hostname = localhost, and reference the variables utilizing double curly braces, akin to @host = {{hostname}}:{{port}}. Invoking a HTTP GET request is finished with the GET command as proven with the next picture. With this command, Visible Studio exhibits a inexperienced arrow that you could click on to invoke this request. The response is proven with the output on the correct aspect of this file. The header from the service in addition to the content material is nice readable:

HTTP File with variables and GET request

One file can include a number of requests – that you just all can set off clicking the inexperienced arrow. Simply be certain together with feedback with three hashes. In any other case, Visible Studio doesn’t ender the file with the inexperienced arrow buttons. Making a recreation by sending a POST request returns a game-id. This id is well copied to the variable @gameid which permits to ship the subsequent POST request to ship a transfer, together with header data. To separate the physique from the header be certain to enter an empty line:

HTTP File with POST

With this you may simply debug immediately from Visible Studio. Simply be certain in case your debug session takes greater than 20 seconds to alter the timout with the setting `Instruments > Choices > Textual content Editor | Relaxation | Superior | Response | Request timeout.

HTP File Settings with Visual Studio

Take away

Visible Studio 2022 has an incredible built-in assist to make HTTP requests to an API. Create a HTTP file, open two home windows side-by-side (do you’ve a large display screen or a number of screens?), and you may simply debug into your API. HTTP Recordsdata are a easy however nice extension to Visible Studio. The dotnet httprepl is a good device which additionally permits to script it.

When you use an earlier model of Visible Studio 2022, you should utilize the Visible Studio extension REST Consumer from Mads Kristensen. Utilizing Visible Studio Code, the Visible Studio REST Consumer relies on the Visible Studio Code extension REST Consumer from Huachao Mao.

Take pleasure in studying and programming!


When you loved this text, I’m grateful in the event you assist me with a espresso. Thanks!



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments