Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
ASP.NET Core 9 Web API Cookbook

You're reading from   ASP.NET Core 9 Web API Cookbook Over 60 hands-on recipes for building and securing enterprise web APIs with REST, GraphQL, and more

Arrow left icon
Product type Paperback
Published in Apr 2025
Publisher Packt
ISBN-13 9781835880340
Length 344 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Luke Avedon Luke Avedon
Author Profile Icon Luke Avedon
Luke Avedon
Garry Cabrera Garry Cabrera
Author Profile Icon Garry Cabrera
Garry Cabrera
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Chapter 1: Practical Data Access in ASP.NET Core Web APIs FREE CHAPTER 2. Chapter 2: Mastering Resource Creation and Validation 3. Chapter 3: Securing Your Web API 4. Chapter 4: Creating Custom Middleware 5. Chapter 5: Creating Comprehensive Logging Solutions 6. Chapter 6: Real-Time Communication with SignalR 7. Chapter 7: Building Robust API Tests: a Guide to Unit and Integration Testing 8. Chapter 8: GraphQL: Designing Flexible and Efficient APIs 9. Chapter 9: Deploying and Managing Your WebAPI in the Cloud 10. Chapter 10: The Craft of Caching 11. Chapter 11: Beyond the Core 12. Index 13. Other Books You May Enjoy

Testing the API in PowerShell

When testing your API, relying solely on tools such as cURL or Swagger can limit your options. In this recipe, we will confirm that the API is correctly implemented via PowerShell. We will retrieve pagination metadata, navigate through pages, and ensure that the returned data is what we expect it to be. We will accomplish this using the built-in Invoke-WebRequest to inspect our custom X-Pagination header. We will type this recipe directly into the PowerShell terminal.

Important note

While we’ll be using PowerShell in this recipe, rest assured that we’ll also cover other popular tools such as Postman throughout this book—equipping you with a diverse set of testing techniques.

Getting ready

Clone the starting code from here: https://github.com/PacktPublishing/ASP.NET-9-Web-API-Cookbook/tree/main/start/chapter01/psTesting. It contains a web API similar to the other APIs we have built in this chapter.

You will need to open PowerShell or use something like Windows Terminal with PowerShell. PowerShell is cross-platform, so you don’t have to be on Windows to follow along with this recipe. The end project folder for this recipe has these commands saved in a PowerShell script if you want to compare. Instead of writing a script file, we will be entering these commands directly into the terminal.

How to do it…

  1. Run the application using the following command:
    dotnet run
  2. Open your PowerShell terminal. Let’s create some variables for your test URL. Save your baseUrl and the endpoint you want to test with pageSize in separate variables:
    $baseUrl = "http://localhost:5148"
    $testEndpoint = "/Products?pageSize=10"
    $fullUrl = $baseUrl + $testEndpoint;

Important note

Remember: your baseUrl has whatever port number dotnet run is serving your API on.

In the terminal, you should now see our API being served on the local host.

Figure 1.6 – dotnet run starting the API on port 5148, your port may be different

Figure 1.6 – dotnet run starting the API on port 5148, your port may be different

  1. Now call the endpoint and save the response in a variable:
    $response = Invoke-WebRequest -Uri $fullUrl -Headers @{"Accept" = "application/json"}

Important note

One alternative way of querying an endpoint in PowerShell is Invoke-RestMethod, which directly converts a JSON response to a PowerShell object. However, Invoke-WebRequest has advantages, as it provides access to more detailed information about the HTTP response, including headers, status codes, and cookies.

  1. Save the pagination metadata in variables:
    $xPaginationHeader = $response.Headers["X-Pagination"]
    $xPagination = $xPaginationHeader | ConvertFrom-Json
  2. Ensure that the correct pagination data is present.

    Figure 1.7 shows the $xPagination variable in our PowerShell terminal:

Figure 1.7 – Pagination metadata displayed in PowerShell

Figure 1.7 – Pagination metadata displayed in PowerShell

  1. Save a URL for NextPage in a variable:
    $nextPageUrl = $baseUrl + $xPagination.NextPageUrl
  2. Call the next page and examine the results:
    $response = Invoke-WebRequest -Uri $nextPageUrl
    $jsonContent = $response.Content | ConvertFrom-Json
    $jsonContent | Format-Table -AutoSize

    Figure 1.8 shows the next page of data displayed in PowerShell:

Figure 1.8 – The next page of data displayed in PowerShell

Figure 1.8 – The next page of data displayed in PowerShell

How it works…

We verified our pagination headers and played around with paged data directly in the terminal. We also formatted response content in PowerShell and learned about Invoke-WebRequest, which is one way we can manipulate our APIs from the terminal. The response from Invoke-WebRequest includes headers, which we accessed to get the X-Pagination custom header. This header contains our pagination metadata such as the total number of pages, the current page, and links to the next and previous page. Invoke-WebRequest lets you access all response headers directly, making it easy to parse a custom header like X-Pagination.

Another similar cmdlet is Invoke-RestMethod, which automatically parses your JSON and returns a smaller response object. The advantage of Invoke-WebRequest is that it can include more information. Invoke-RestMethod is fantastic for simple REST API interactions but Invoke-WebRequest can be better suited for complex interactions.

You have been reading a chapter from
ASP.NET Core 9 Web API Cookbook
Published in: Apr 2025
Publisher: Packt
ISBN-13: 9781835880340
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime
Modal Close icon
Modal Close icon