(C# ASP.NET Core) Testing a POST Web Api with HttpRepl

In this tutorial we shall test a POST Web Api - and create a new record. So we shall have to first establish a connection between httprepl and our web server, and, then we will have to post the json for the new record that we have to create on the server. Let's see how!
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

The Web Api that we have to call

Let's first have a look at the web api that we have to call.

Open the WebApi project that we have been discussing in this chapter. This project is already available in your downloads. Locate the program.cs file and scroll to the part that you are seeing here.

// POST WebApi from Program.cs 
// the webapi project is in your downloads 
app.MapPost("/doctor", async (MyApiContext ctx, DoctorDTO doctor) =>
{

  Doctor doc = new Doctor()
  {

    Name = doctor.Name,
    Fees = doctor.Fees,
    EMail = "assign-any"
  };

  ctx.Doctors.Add(doc);

  // success assumed for tutorial purposes 
  await ctx.SaveChangesAsync();

  return Results.Created(
      $"/doctor/{doctor.Id}", new DoctorDTO(doc));

});

We shall make a call to this POST web api. It responds with HTTP 201 and a json of the newly created record. Also, recall that, as we explained in the previous tutorial, the endpoint of our web api is "doctor" because it appears as the first segment of the route templates of all our web api.

Run the project to open the home page. This ensures that the server is up and running. Take note of the address - it will be required for making a connection between httprepl and this server.

Video Explanation (see it happen!)

Please watch the following youtube video:

Connect http repl

Next, we have to make a connection between http repl and our web api server. Come to visual studio and use the menu Tools > Command Line > Developer Command Prompt and open the http repl console window. Type the command httprepl followed by the server address as you are seeing here. Hit enter and wait for the connection.

If everything goes right, the command prompt will now show the address of your server as you are seeing here.


https://localhost:7264/>

Next we have to connect to the "doctor" endpoint - so type the command cd doctor and hit enter.


cd doctor

If everything goes right, then you should now be connected to the "doctor" endpoint, as you are seeing here.


https://localhost:7264/doctor>

Making the Call

Now we can execute the call to our api.

At the command prompt type POST followed by the json of the record to be created, as you are seeing here. Notice that the parameter is put inside a double quotes even though the json itself includes double quotes


https://localhost:7264/doctor> POST  --content "{"name":"doc 4","fees":130}"

// parameter json is in double quotes even though the  
// json itself is also in double quotes 

Hit enter, and wait for the response from the server. We verify that we have received a json of the newly created record with a response of HTTP 201 - as expected.


https://localhost:7264/doctor> POST  --content "{"name":"doc 4","fees":130}"

HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Date: Sat, 04 Jun 2022 06:18:57 GMT
Location: /doctor/0
Server: Kestrel
Transfer-Encoding: chunked

{
  "id": 4,
  "name": "doc 4",
  "fees": 130
}


This Blog Post/Article "(C# ASP.NET Core) Testing a POST Web Api with HttpRepl" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.