Rest Client Utils. Rest Query Builder
$ dotnet add package OutWit.Common.RestOutWit.Common.Rest is a robust library designed to simplify and streamline HTTP client interactions in .NET applications. By leveraging a modular and extensible architecture, it offers powerful abstractions for building and executing RESTful requests, handling responses, and managing query parameters with ease.
The QueryBuilder class provides a fluent API for constructing URL query strings dynamically. It supports multiple parameter types, including primitives, enumerations, and collections.
var queryBuilder = new QueryBuilder()
.AddParameter("name", "John Doe")
.AddParameter("age", 30)
.AddParameter("tags", new[] { "developer", "blogger" });
string query = await queryBuilder.AsStringAsync();
Console.WriteLine(query); // Output: name=John%20Doe&age=30&tags=developer,blogger
The RestClient class provides an abstraction for HTTP operations, including GET, POST, and SEND requests. It simplifies common tasks like deserialization and content creation while allowing advanced configuration with fluent methods.
var client = RestClientBuilder.Create()
.WithBearer("your-token")
.WithHeader("Custom-Header", "HeaderValue");
var result = await client.GetAsync<MyResponseType>("https://api.example.com/resource");
The RestClientException class encapsulates HTTP status codes and response content, making error handling intuitive and detailed.
try
{
var result = await client.GetAsync<MyResponseType>("https://api.example.com/invalid-resource");
}
catch (RestClientException ex)
{
Console.WriteLine($"Error: {ex.StatusCode}, Content: {ex.Content}");
}
Integrated with Newtonsoft.Json, the library supports efficient serialization and deserialization of request and response content.
var response = await httpResponseMessage.DeserializeAsync<MyResponseType>();
The library includes utilities for building requests with advanced features like:
Install the package via NuGet:
Install-Package OutWit.Common.Rest
Licensed under the Apache License, Version 2.0. See LICENSE.
If you use OutWit.Common.Rest in a product, a mention is appreciated (but not required), for example: "Powered by OutWit.Common.Rest (https://ratner.io/)".
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
You may not: