A simple and flexible library for handling RESTful API requests using pure C# HttpClient
and Newtonsoft.Json
. This library allows you to easily send GET
, POST
, PUT
, and DELETE
requests with support for headers, authentication, and custom content types.
- Ability to send requests and retrieve responses as raw JSON or deserialized objects.
- Lazy initialization of
HttpClient
to ensure memory efficiency. - Support for adding and removing headers.
- Authorization support (e.g., Bearer tokens, custom schemes).
- Easy timeout configuration.
- Chainable
HttpRequestMessageBuilder
for constructing custom requests.
Here is the current roadmap for future updates.
- Exception Handling: Currently, this library does not handle exception handling. Future updates may include built-in support for managing exceptions to improve usability and robustness.
- JsonName Attribute: Attribute to change the name of a field in the object scheme
this makes it so you can Parse a Json with a scheme
public class Student { [JsonName("fullName")] string Name { get; set; } [JsonName("age")] int Age { get; set; } }
{ "fullName": "MyName", "age": 23 }
to the Student class even if the fields are named differently.
-
Download the source code or clone the repository.
git clone https://github.com/DarkenSoda/RestHandler.git
-
Ensure that you have
Newtonsoft.Json
installed in your project. You can install it via NuGet:Install-Package Newtonsoft.Json
or using CLI
dotnet add package Newtonsoft.Json
-
Add the
DarkenSoda.RestHandler
namespace to your project.
You can send a GET
request and get the response as a deserialized object or as a raw string.
using DarkenSoda.RestHandler;
using System.Threading.Tasks;
// make sure this has the same scheme as the api
public class MyResponseType
{
public int TestNumber { get; set; }
}
public async Task GetExample()
{
MyResponseType response = await ApiRequest.GetAsync<MyResponseType>("https://api.example.com/data");
// Raw JSON response
string jsonResponse = await ApiRequest.GetRawStringAsync("https://api.example.com/data");
}
To send a POST
request with content:
using DarkenSoda.RestHandler;
using System.Threading.Tasks;
public async Task PostExample()
{
MyContent requestData = new MyContent() { Name = "John", Age = 30 };
MyResponseType response = await ApiRequest.PostAsync<MyResponseType, MyContent>("https://api.example.com/data", requestData);
// Raw JSON response
string jsonResponse = await ApiRequest.PostRawStringAsync<MyContent>("https://api.example.com/data", requestData);
}
Before sending any requests, you can configure the base URL and headers.
using DarkenSoda.RestHandler;
// Set the base address for all requests
HttpClientManager.SetBaseAddress("https://api.example.com");
// Add custom headers
HttpClientManager.AddHeader("Custom-Header", "HeaderValue");
If your API requires an authorization token, you can include it in the request:
using DarkenSoda.RestHandler;
HttpClientManager.AddAuthorizationHeader("Bearer", "your-token");
To remove a specific header or authorization from your request:
HttpClientManager.RemoveHeader("Custom-Header");
// or
HttpClientManager.ClearHeaders(); // clear all headers
HttpClientManager.RemoveAuthorizationHeader();
To configure the request timeout:
HttpClientManager.SetTimeout(30); // Timeout in seconds
This example shows how you can use everything together.
Note you can set the Headers and Authorization for the entire library using the HttpClientManager
or send them per use as shown below:
static async Task Main(string[] args)
{
// Set the base address for the HttpClient
HttpClientManager.SetBaseAddress("https://api.example.com/");
// Define the headers, scheme, and token
var headers = new Dictionary<string, string>
{
{ "Custom-Header", "HeaderValue" },
{ "Another-Header", "AnotherValue" }
};
string token = "your-jwt-token";
string scheme = "Bearer";
// Example: Sending a GET request with optional headers and authorization
string getUrl = "resource/get";
var response = await ApiRequest.GetAsync<MyResponseType>(getUrl, scheme, token, headers);
Console.WriteLine("GET Response: " + response);
// Example: Sending a POST request with headers, authorization, and content
string postUrl = "resource/post";
MyContent postContent = new MyContent()
{
Name = "Example",
Value = 123
};
var postResponse = await ApiRequest.PostAsync<MyResponseType2, MyContent>(postUrl, postContent, scheme, token, headers);
Console.WriteLine("POST Response: " + postResponse);
}
Contributions are welcome! If you'd like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix (
git checkout -b feature-name
). - Add your feature or fix and update the CONTRIBUTORS file with your name.
- Commit your changes (
git commit -m 'Add feature'
). - Push to the branch (
git push origin feature-name
). - Open a pull request.
By following these steps, your contribution and name will be recognized once your pull request is merged.
This project is licensed under the MIT License - see the LICENSE file for details.