Katharsis.Http 1.0.0

dotnet add package Katharsis.Http --version 1.0.0
                    
NuGet\Install-Package Katharsis.Http -Version 1.0.0
                    
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="Katharsis.Http" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Katharsis.Http" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Katharsis.Http" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Katharsis.Http --version 1.0.0
                    
#r "nuget: Katharsis.Http, 1.0.0"
                    
#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.
#:package Katharsis.Http@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Katharsis.Http&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Katharsis.Http&version=1.0.0
                    
Install as a Cake Tool

Katharsis.Http

Katharsis.Http is a library to help you write your HTTP requests code easier.

Documentation

Please read the following documentation to understand how to use Katharsis.Http library.

The Client

To make your first HTTP request you need to create HTTP Client. HTTP Client is the object that handles all HTTP requests. In Katharsis.Http class KatharsisClient instance of object handles all HTTP request methods.

To create The Client you should call KatharsisClient constructor with base url address.

using Katharsis.Http;

const string BASE_URL = "https://reqres.in";
KatharsisClient client = new KatharsisClient(BASE_URL);

The above code will create client instance of object with base url, so every request that will be send by this client will try to request BASE_URL address.

Default Headers For The Client

You can also attach default headers that will be sent to every request using that specific client.

using Katharsis.Http;

var defaultHeaders = new Dictionary<string, string>()
{
    ["key"] = "PR1V4T3K3Y"
};

const string BASE_URL = "https://reqres.in";
KatharsisClient client = new KatharsisClient(BASE_URL, defaultHeaders);
Custom Body Serialization

Sometimes you need to serialize your HTTP body content in a specific way. Katharsis.Http allows you to do that. KatharsisClient object contains property called Serializer that implements ISerializer interface. You can change implementation of that property by creating your own class that will ipmlement such interface e.g.:

using Katharsis.Http.Interfaces;
using Newtonsoft.Json;

internal class JsonSerializer : ISerializer
{
    public string Serialize(object body) => JsonConvert.SerializeObject(body);
}

The above class is using Newtonsoft.Json library to serialize objects. Serialize(object body) method returns string that will be later on attached to request's HTTP content. There are two ways of changing the Serializer property of your client: By constructor and by setter

Constructor
const string BASE_URL = "https://reqres.in";
var jsonSerializer = new JsonSerializer();
KatharsisClient client = new KatharsisClient(BASE_URL, jsonSerializer);
Setter
const string BASE_URL = "https://reqres.in";
KatharsisClient client = new KatharsisClient(BASE_URL);

var jsonSerializer = new JsonSerializer();
client.Serializer = jsonSerializer;

The Request

To invoke your first request to BASE_URL using The Client, you can use several methods based on what type of request you would like to make. Each of method will always return KatharsisResponse instance of object containing:

  • Content
  • Exception (If any exception has been thrown internally in method, in other case it will be null)
  • Request (Information about request that has been sent)
  • Status

To invoke simple GET HTTP Request you can call either Get() or GetAsync() (for asynchronous operation) method with resource parameter.

KatharsisResponse response = client.Get("api/users?page=2");

or

KatharsisResponse response = await client.GetAsync("api/users?page=2");

Katharsis.Http handles 5 basic methods for GET, POST, PUT, DELETE, PATCH listed below:

  • Get() or GetAsync()
  • Post() or PostAsync()
  • Put() or PutAsync()
  • Delete() or DeleteAsync()
  • Patch() or PatchAsync()

For all of these methods you can pass parameters, which resource is required.

Body

To attach Body Content to the request, you can simply pass it to the method. E.g.:

var picture = new
{
    Base64String = "po123u9asdjd1ej12j!JSSasdjJSA0sJd=="
};

KatharsisResponse response = client.Post("api/profile/uploadPicture?profileId=1", picture);
Additional Headers

Besides the fact that The Client has Default Headers you can also attach Additional Headers to your request. Let's sey that for example Web API requires from us Authorization Key - in that case we can attach it to our HTTP Client as I described in The Client part of documentation. But for a the specific resource which might be /api/profile/uploadPicture Web API requires from us to send additional header which is Content-Type with value of x-www-form-urlencoded.

The code below shows you how you can handle it.

var defaultHeaders = new Dictionary<string, string>()
{
    ["key"] = "PR1V4T3K3Y"
};

const string BASE_URL = "https://reqres.in";
KatharsisClient client = new KatharsisClient(BASE_URL, defaultHeaders);

var picture = new
{
    Base64String = "po123u9asdjd1ej12j!JSSasdjJSA0sJd=="
};

var additionalHeaders = new Dictionary<string, string>
{
    ["Content-Type"] = "x-www-form-urlencoded"
};

KatharsisResponse response = client.Post("api/profile/uploadPicture?profileId=1", picture, additionalHeaders);
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 206 8/20/2023