Introduction
Many times it is necessary to test the web APIs that have been produced. Sometimes it is necessary to test these web APIs in environments other than the development environment. There are various tools for this such as Postman, cURL, Swagger and many more. But among all these tools, one tool with all, has attractive differences. This tool is HTTPRepl.
What is HTTPREPL?
The HTTP Read-Eval-Print Loop (REPL) is a lightweight and cross-platform command-line tool that's supported everywhere .NET Core is supported. Using this tool you can start discovering and exploring web APIs just like as you are exploring files and folders in command prompt. HTTPRepl used for making HTTP requests to test ASP.NET Core web APIs and also non-ASP.NET Core web APIs and view their results. This tool is capable of testing web APIs hosted in any environment, including localhost and Azure App Service. HTTPRepl has great functionality when is used with web APIs having directory structure based on OpenAPI description.
How to work with HTTPREPL
In the following, we will get to know some HTTPRepl commands:
Install
Install HTTPRepl using following command:
dotnet tool install -g Microsoft.dotnet-httprepl
Using the above command HTTPRepl is being installed globally(using -g switch), allows you to run it from everywhere, however, it is not necessary to install it globally.
Suppose that there is OpenAPI friendly web APIs located at the https://petstore.swagger.io address. Also, the OpenAPI description file is accessible using https://petstore.swagger.io/v2/swagger.json url.
Connect
The first step in using HTTPRepl is to connect to the web APIs. In order to connect to web API, which has OpenAPI description file, you can use the following commands:
httprepl
connect https://petstore.swagger.io --openapi https://petstore.swagger.io/v2/swagger.json
After executing httprepl command in first line, you will enter the httprepl. By executing the connect command you are attempting to connect to web API. The first argument for this command is the root url.
You can ignore --openapi switch if your web APIs do not have any OpenAPI description.
List available endpoints
If your web API has OpenAPI description, just like as the above web API, then you can use ls or dir commands to list all available endpoints as below:
Navigate to the endpoint
As it is clear in above image, there are three different endpoints available in the root. You can use cd command to get inside an endpoint. for example:
cd store
After executing ls inside the store, you can view the available endpoints located in store path. After exploring endpoints, you can call them based on their type. For example, in order to call a GET endpoint, located in store path, you can execute the following command:
get inventory
Or you can navigate to the inventory and then call it as below:
What if we want to call a GET web API with parameter? In fact, it has no different with the previous commands, and you can pass your parameter using one of the following approaches:
#Method 1
get store/order/1
# Method 2
cd store/order
get 1
#Method 3
cd store/order/1
get
If you want to modify some data using POST, PUT or other methods, you can use the following command: (Suppose that you are in store/order path)
post --content "{"id":1000,"petId":100,"quantity":2,"shipDate":"2023-07-25","status":"placed","complete":true}"
You can set the editor to be opened while you are modifying the data. For example, suppose that I am going to open the Notepad.exe whenever the user wants to modify the data as below:
pref set editor.command.default "C:\windows\System32\notepad.exe"
By setting the default editor, whenever user wants to modify the data, Notepad.exe will be shown as below:
After setting the values in Notepad.exe you can close it. After closing Notepad.exe, the web API will be executed automatically.
If you want to read more about HTTPRepl you can check Learn.Microsoft.com