Friday, December 11, 2020

HttpRepl essentials

HttpRepl is a light-weight utility from Microsoft used to test REST APIs and view their results. It can run on any O/S that .NET Core (or .NET 5 and later) runs on. HttpRepl is an open-source project on GitHub at https://github.com/dotnet/HttpRepl. It is an alternative to Postman.

Companion video: https://youtu.be/RLdnXN4OAPg

To install the HttpRepl, run the following command from within a terminal window:

dotnet tool install -g Microsoft.dotnet-httprepl

To update HttpRepl to the latest version, enter the following command:

dotnet tool update -g Microsoft.dotnet-httprepl

On Windows, this tool gets installed at:

%USERPROFILE%\.dotnet\tools\httprepl.exe

Let us test  HttpRepl with a REST API service located at https://api4all.azurewebsites.net. From within a terminal window, enter the following command:

httprepl https://api4all.azurewebsites.net

HttpRepl

You are advised to associate the HttpRepl utility with a text editor so that you can compose your POST & PUT requests. The below terminal window command will associate Notepad.exe (in Windows) with HttpRepl:

pref set editor.command.default "C:\Windows\system32\notepad.exe"

pref set editor.command.default

Enter dir (or ls) :

HttpRepl allows you to navigate through your API tree. In our current service, all services are under /api. Therefore, change directory to api then check the contents with:

cd api
dir
dir

Now, let’s checkout Students. Therefore, change to the Students location and inspect what is available there with:

cd Students
dir

dir

NOTE: HttpRepl is case-sensitive. This means that “cd students” will generate an error.

This shows us that we can do GET, POST, PUT and DELETE. Entering the id is necessary for DELETE, PUT and retrieving one Student (GET by id).

Let us retrieve all Students data by entering GET:

GET

How about we try POST. Just enter POST and the editor we configured earlier on pops up with a JSON template:
POST

Enter some data then simply save & close your editor. I entered the following then saved & closed the editor. 
POST

This was the output I got:

POST
Enter GET to ensure that data was indeed inserted:
GET

Let’s do a GET by id by entering the following:

GET A00555555

This is the result:
GET by id

How about we do an update. Enter:

PUT A00555555

This opens the editor again. However, the existing data is not displayed. This is what I got:
PUT

I entered data as follows, using the same ID as before, and changing only school to nursing. I then saved the document before closing it:


PUT

This is the output I received:

PUT result

I then did a GET to see all students:

GET all result

Finally, let us delete our student by entering:

DELETE A00555555

You should see the following response:

DELETE by id

Now when we do a GET, there will be no student with ID = A00555555:
GET all

To exit out of HttpRepl, simply type exit.

I hope you found HttpRepl a useful utility.





No comments:

Post a Comment