Skip to content

Quick Start

Alessandro Morvillo edited this page Aug 8, 2026 · 2 revisions

Quick Start

This example sends a GET request, deserializes JSON into a model, and handles unsuccessful results.

1. Define a response model

Restling supports models annotated for System.Text.Json, Newtonsoft.Json, or models that use conventional property names.

public sealed class TodoItem
{
    public int Id { get; set; }
    public string? Title { get; set; }
    public bool Completed { get; set; }
}

2. Send a typed GET request

using AMDevIT.Restling.Core;

RestlingClient client = new();
RestRequestResult<TodoItem> result;

result = await client.GetAsync<TodoItem>("https://api.example.com/todos/1",
                                         cancellationToken: cancellationToken);

3. Handle the result

if (result.IsSuccessful && result.Data is not null)
{
    Console.WriteLine($"{result.Data.Id}: {result.Data.Title}");
}
else
{
    Console.WriteLine($"HTTP status: {result.StatusCode}");
    Console.WriteLine($"Error: {result.Exception?.Message}");
    Console.WriteLine($"Response: {result.Content}");
}

IsSuccessful is true only when no exception was captured and the response has a supported successful HTTP status code. See Responses and Errors for all result properties.

POST a JSON body

public sealed record CreateTodoRequest(string Title, bool Completed);

CreateTodoRequest payload = new("Try Restling", false);
RestRequestResult<TodoItem> result;

result = await client.PostAsync<TodoItem, CreateTodoRequest>("https://api.example.com/todos",
                                                             payload,
                                                             cancellationToken: cancellationToken);

For PostAsync<TResponse, TRequest> and PutAsync<TResponse, TRequest>, the first generic argument is the response model and the second is the request model.

Add a bearer token to one request

using AMDevIT.Restling.Core.Network;

RequestHeaders headers = new(new AuthenticationHeader("Bearer", accessToken));
headers.Headers.Add("X-Correlation-ID", correlationId);

RestRequestResult<TodoItem> result = await client.GetAsync<TodoItem>("https://api.example.com/todos/1",
                                                                     headers,
                                                                     cancellationToken: cancellationToken);

Use Client Configuration when headers or other settings should apply to all requests.

Clone this wiki locally