Testing an ASP.NET Core 5.0 (preview) with Postman

mariocatch
4 min readAug 17, 2020

--

Postman is a powerful tool for quickly testing local or external http endpoints. In this tutorial we will setup Postman, Visual Studio 2019 Preview, .NET 5 preview, and create an ASP.NET Core 5.0 Web API.

Setup:

Github repo: https://github.com/mariocatch/PostmanAPI-Medium

Create the ASP.NET Core 5.0 Web API:

dotnet new webapi -o PostmanAPI
cd ./PostmanAPI
dotnet build
./PostmanAPI.csproj

Visual Studio 2019 should be opened with your PostmanAPI project. Press F5 to run it and make sure it works.

It should open a browser to a localhost URL of something like https://localhost:44347/weatherforecast. The browser should be showing JSON of a weather response.

Open up Postman, login or create an account if you haven’t already done so. Click the New button in the top-left, and select Request.

Give the request a name and add a new Collection to save our test requests to.

Copy/paste the URL from your browser’s debugging session to the Postman textbox labeled Enter request URL.

Click Send.

If you receive an error stating Error: Unable to verify the first certificate, then click the Disable SSL verification button, or go to File > Settings and turn off the SSL certificate verification.

Congratulations! You’ve just sent an HTTP GET request to your ASP.NET Core Web API’s /weatherforecast endpoint. The result in the body section of Postman should show an array of weather result objects.

To finish up, let’s modify the api to support HTTP POST requests for adding new weather data.

Go over to Visual Studio and stop the debugging session if you haven’t already. Open WeatherForecastController.cs, and notice that our current Get method returns a local array containing random weather data. This won’t be very useful if we’re trying to add to the weather array from another endpoint, so let’s raise the state of our weather data to the controller itself.

private static Random _rng = new Random();private static List<WeatherForecast> _weatherForecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = _rng.Next(-20, 55),
Summary = Summaries[_rng.Next(Summaries.Length)]
}).ToList();
[HttpGet]
public IEnumerable<WeatherForecast> Get() => _weatherForecasts;

To keep it simple, we’ve just moved the prior weather array to a static list on the controller. In a real world you would create a service that was responsible for interacting with weather data, and inject that service into this controller.

Now we can add our HTTP Post action which will handle receiving a WeatherForecast object and adding it to the controller’s in-memory list.

[HttpPost]
public void Post(WeatherForecast weatherForecast) => _weatherForecasts.Add(weatherForecast);

Run your app with F5, and head back over to Postman. Hit the + to open a new tab (request). Change the action from GET to POST, leave the URL the same. Open the Body tab of the request, set the request type to raw, drop down the content type option at the end of that row and select JSON. In the end it should look like this:

Now inside the text area, add a test JSON object that matches the schema of a WeatherForecast. Here’s one you can test with:

{
"date": "2020-08-20",
"temperatureC": -42,
"temperatureF": -43.6,
"summary": "Foo"
}

Click Send, and note the response status code in Postman as 200 OK. Usually you would setup your endpoint to indicate this was actually a 201 Created, to state that something was added as a result of an HTTP Post. But this is fine for now.

To test that our post actually worked, click back on our previous request’s tab in Postman (the one that was a GET) and click Send.

Success! We’ve managed to setup Postman to make GET and POST requests to an ASP.NET 5 Core Web API.

--

--

mariocatch

Software Engineer. Web Developer. Creator of Cherish. Gamer. Husband. Father. Human.