I had a reader email me about using Postman with ASP.NET Core API base on this post from a couple of years ago. Rather than working through that their specific issues are with that code, I thought it might be more helpful to write a post on creating a super basic ASP.NET Core API and use Postman to test it.
We are going to use the .NET CLI to create and run API project so no Visual Studio or other IDE will be needed. The first step is to open a command prompt and navigate to (or create) the directory where you want the API project to live. Run the following command to create the API project.
dotnet new webapi
The webapi template creates a
ValuesController with a
Get action that returns an array with two values in it which we will be using as our test endpoint.
After the process finished we can now run the project using the following command.
After the run command, you should see something like the following.
Hosting environment: Production Content root path: C:\YourProjectPath\ApiTest Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.
The key bit in the above you need to look for is the Now listening on line as that is the URL we will need to use in Postman to test.
Testing with Postman
Postman is a great tool that to use when developing an API. It allows me to exercise all the functions of the API before any clients have been built. You can do some of the same things using a browser, but Postman was built for this type of usage and it shows. Postman is free and you can grab it here.
Run Postman and you will see something similar to the following screenshot.
For our simple test we want to do a Get request, which is the default, so all we need to do is past the URL from above into the address box and add in the route to the controller we are trying to test. For our sample to test the
Get action on the
ValuesController our URL ends up being http://localhost:5000/api/values.
Click the Send button and the results will show the lower area Postman (the large red box in the screenshot).
This is the simplest setup I could think of to get up and going with Postman and ASP.NET Core. Postman has so many more functions than I showed in this post so I hope this will be a good jumping off point for you all to learn more about this great tool.