This package provides a simple and efficient way to make HTTP requests using methods like GET, POST, PUT, PATCH, and DELETE. It handles serialization, deserialization, and response management.
$ dotnet add package AsthaIT.RestClientA lightweight and easy-to-use HTTP client for .NET applications that provides a clean interface for making HTTP requests. This package simplifies API integrations by providing strongly-typed responses and a fluent API design.
dotnet add package AsthaIT.RestClient
First, add the necessary using statement:
using AIT.Packages.RestClient;
using AIT.Packages.RestClient.Response;
// Simple GET request
var response = await AITRestClient.GetAsync<UserDto>("https://api.example.com/users/1");
// GET request with custom headers
var headers = new Dictionary<string, string>
{
{ "Authorization", "Bearer your-token" },
{ "Accept", "application/json" }
};
var response = await AITRestClient.GetAsync<UserDto>(
"https://api.example.com/users/1",
headers
);
if (response.IsSuccess)
{
var user = response.Data;
Console.WriteLine($"User name: {user.Name}");
}
// Request model
var newUser = new CreateUserDto
{
Name = "John Doe",
Email = "john@example.com"
};
// Simple POST request
var response = await AITRestClient.PostAsync<CreateUserDto, UserDto>(
"https://api.example.com/users",
newUser
);
// POST with headers
var headers = new Dictionary<string, string>
{
{ "Authorization", "Bearer your-token" }
};
var response = await AITRestClient.PostAsync<CreateUserDto, UserDto>(
"https://api.example.com/users",
newUser,
headers
);
var formData = new Dictionary<string, object>
{
{ "username", "johndoe" },
{ "password", "secretpassword" },
{ "remember", true }
};
var response = await AITRestClient.PostAsync<LoginResponseDto>(
"https://api.example.com/login",
formData
);
var updateUser = new UpdateUserDto
{
Name = "John Updated",
Email = "john.updated@example.com"
};
var response = await AITRestClient.PutAsync<UpdateUserDto, UserDto>(
"https://api.example.com/users/1",
updateUser
);
var patchUser = new PatchUserDto
{
Name = "John Patched"
};
var response = await AITRestClient.PatchAsync<PatchUserDto, UserDto>(
"https://api.example.com/users/1",
patchUser
);
var response = await AITRestClient.DeleteAsync<DeleteResponseDto>(
"https://api.example.com/users/1"
);
All requests return an APIResponse<T> object that contains:
public class APIResponse<T>
{
public bool IsSuccess { get; set; } // Indicates if the request was successful
public HttpStatusCode StatusCode { get; set; } // The HTTP status code
public T? Data { get; set; } // The response data (if any)
public string? ErrorMessage { get; set; } // The error message (if any)
}
Example of handling responses:
var response = await AITRestClient.GetAsync<UserDto>("https://api.example.com/users/1");
if (response.IsSuccess)
{
var user = response.Data;
Console.WriteLine($"Successfully retrieved user: {user.Name}");
}
else
{
Console.WriteLine("StatusCode: {response.StatusCode}");
Console.WriteLine("ErrorMessage: {response.ErrorMessage}");
}
MIT License
Developed and maintained by AIT.