EnhancedWebRequest 1.1.3

dotnet add package EnhancedWebRequest --version 1.1.3                
NuGet\Install-Package EnhancedWebRequest -Version 1.1.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EnhancedWebRequest" Version="1.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EnhancedWebRequest --version 1.1.3                
#r "nuget: EnhancedWebRequest, 1.1.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EnhancedWebRequest as a Cake Addin
#addin nuget:?package=EnhancedWebRequest&version=1.1.3

// Install EnhancedWebRequest as a Cake Tool
#tool nuget:?package=EnhancedWebRequest&version=1.1.3                

Enhanced Web Request

Yet another web API client for .net 8

Introduction

This package provides basic web API functionality using HttpClient with HttpRequestMessage and HttpResponseMessage in .net 8.

Methods handling entity data in the JSON format use the System.Text.Json library.

There are significant changes in version 1.1.1 of the package.

Example Usage

There are three constructors to the EnhancedWebRequest class:

  • public EnhancedWebRequest(HttpClient httpClient)
  • public EnhancedWebRequest(HttpClientHandler clientHandler)
  • public EnhancedWebRequest(string baseUrl, EnhancedWebRequestOptions options)

Assuming we have an existing HttpClient object called httpClient and an entity of type MyClass, we can send a PUT request to update the entity on the remote service using JSON:

var webReq = new EnhancedWebRequest(httpClient);
var entity = new MyClass() { Prop = "value", Id = 1234 };
var response = await webReq.PutJsonEntity(entity, $"https://api.example.com/rest/entity/{entity.Id}");
var updatedEntity = await response.AsJsonEntityAsync<MyClass>();

Methods with an optional url parameter handle the value of that parameter as follows:

  1. If the URI passed to the method is an absolute URI, it will be used as the request URI, overriding the base URI in the class if one is present.
  2. If the URI passed to the method is a relative URI, it will be concatenated with the existing base URI in the class, and the resulting URI will be used as the request URI.
  3. If the URI passed to the method is a relative URI and no base URI is present in the class, an exception is thrown.
  4. If the URI passed to the method is null or empty string, the base URI in the class will be used as the request URI. If no base URI is present in the class, an exception is thrown.

Extension Methods

This package provides a number of useful extension methods for working with the HttpRequestMessage and HttpResponseMessage objects.

In the examples below, assume we already have an instance of the EnhancedWebRequest class called webReq with a base URI of https://api.example.com/rest set.

To de-serialize a response from JSON into a list of objects of type MyClass with a conditional request:

webReq.NotModified += (obj, e) => 
{
  Debug.WriteLine($"Content at {e.Url} has not been modified.");
};

var response = await webReq.GetIfModifiedSince(DateTime.Now.AddMinutes(-30), "/entities");

if (response.HasContent() && response.IsContentType(MediaTypeNames.Application.Json)) 
{
  var entities = await response.AsJsonEntitiesAsync<MyClass>();
  // Do something with entities
}

To handle errors (non-success status codes) received from a remote HTTP endpoint:

webReq.ErrorStatusCode += (obj, e) => 
{
  Debug.WriteLine($"Error making API call: {e.StatusCode} was returned.");
  if (e.ResponseMessage.IsContentType("application/problem+json"))
  {
    var problemJson = e.ResponseMessage.AsJsonEntityError<MyProblemClass>();
    // Do something with the error
  }
};

var entity = new MyClass() { Prop = "value", Id = 1234 };
var response = await webReq.PutJsonEntityIfMatch(entity, "e34ab3daa2f", false, "/entity");

if (response.HasContent()) 
{
  var newEntity = await response.AsJsonEntityAsync<MyClass>();
  // Do something with the updated entity
}

Issues

There are definitely more robust libraries out there for interacting with web APIs. This package is still in its infancy.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EnhancedWebRequest:

Package Downloads
NpiRegistry

A client package for interacting with the National Provider Identifier (NPI) registry API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 36 2/10/2025
1.1.2 26 2/10/2025
1.1.1 37 2/9/2025
1.0.7 46 2/8/2025
1.0.6 40 2/8/2025
1.0.5 35 2/8/2025
1.0.4 33 2/8/2025
1.0.3 68 1/25/2025
1.0.2 59 1/25/2025