Calling Web APIs using the dotnet CLI and HTTP Files with Visual Studio

Visual Studio 2022 17.5 includes new HTTP client tooling. This tool makes it easy to create API calls directly from Visual Studio, with a great output of the API results. It’s easy to directly debug API invocations with this tool. Another tool which I’m using since some years is the HTTP REPL which is available as a dotnet tool. This article shows how both of these tools.

Resting wild seals

HTTP Repl

A tool I’m already using some years is microsoft.dotnet-httprepl that is available as a .NET tool. To install it globally for the current user, you can use dotnet tool install microsoft.dotnet-httprepl -g.

When this tool is installed, you can use the httprepl command to connect to an API when OpenAPI is available, list all the APIs available, and do HTTP requests. Let’s get into a sample. Invoking httprepl without passing a connection, this tool is started disconnected.

To see all the available commands, type help. To connect to an API, use the connect command. The following command connects to the API at https://localhost:7261. You get the information if OpenAOI description was found. If this is the case, you can walk through the different APIs and use the ui command to open the OpenAPI UI page.

dotnet httprepl - Connect

The OpenAPI (Swagger) UI used with the sample application is shown with the next image.

OpenAPI (Swagger) UI

With the ls command, you can list all the APIs available. You can walk through the APIs using the cd command. The ls command shows the enpoints available with the current path. Using the recursive (-r) option, you can see all the endpoints available:

dotnet httprepl - list

Let’s send some requests. With the get command, you can send a GET request to the API. The first get request to the /games API returns an empty array because no game was created yet. Using the post command to send a POST request creates a new game. To pass JSON infomration with the body of the POST request, I’m submiting the content of the file creategame.json using the -f option. If you don’t supply the filename, an editor is opened where you can fill out the content of the body. You can configure the editor to use with the pref (preferences) command.

dotnet httprepl - get and post

Visual Studio Integrated HTTP Client

With Visual Studio 2022 17.5, a new HTTP client tool is available. This tool is integrated into Visual Studio and makes it easy to create API calls directly from Visual Studio.

To use this tool, create a text file with the file extension .http. An item template HTTP File is available:

HTTP File Item Template

Within this file, you can create variables prefixed with @ such as @hostname = localhost, and reference the variables using double curly braces, such as @host = {{hostname}}:{{port}}. Invoking a HTTP GET request is done with the GET command as shown with the following image. With this command, Visual Studio shows a green arrow that you can click to invoke this request. The response is shown with the output on the right side of this file. The header from the service as well as the content is good readable:

HTTP File with variables and GET request

One file can contain multiple requests – that you all can trigger clicking the green arrow. Just make sure including comments with three hashes. Otherwise, Visual Studio does not ender the file with the green arrow buttons. Creating a game by sending a POST request returns a game-id. This id is easily copied to the variable @gameid which allows to send the next POST request to send a move, including header information. To separate the body from the header make sure to enter an empty line:

HTTP File with POST

With this you can easily debug directly from Visual Studio. Just make sure if your debug session takes more than 20 seconds to change the timout with the setting `Tools > Options > Text Editor | Rest | Advanced | Response | Request timeout.

HTP File Settings with Visual Studio

Take away

Visual Studio 2022 has a great built-in support to make HTTP requests to an API. Create a HTTP file, open two windows side-by-side (do you have a wide screen or multiple monitors?), and you can easily debug into your API. HTTP Files are a simple but great extension to Visual Studio. The dotnet httprepl is a great tool which also allows to script it.

If you use an earlier version of Visual Studio 2022, you can use the Visual Studio extension REST Client from Mads Kristensen. Using Visual Studio Code, the Visual Studio REST Client is based on the Visual Studio Code extension REST Client from Huachao Mao.

Enjoy learning and programming!

Christian

If you enjoyed this article, I’m thankful if you support me with a coffee. Thanks!

Buy Me A Coffee

More Information

More information about programming Web API Services is available in my book and my workshops.

Visual Studio 2022 17.5 Released

REST Client Visual Studio Extension

REST Client GitHub Repo

Visual Studio Code REST Client from Huachao Mao

HTTP Repl

Read more about Web APIs in my book Professional C# and .NET – 2021 Edition

See Chapter 25, "Services".

Trainings

Sample source code

Photo 5309377 © Basilisk | Dreamstime.com

Advertisement

3 thoughts on “Calling Web APIs using the dotnet CLI and HTTP Files with Visual Studio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.