Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created Nov 3, 2021
Embed
What would you like to do?
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
var httpClient = new HttpClient(new ProblemDetailsHttpMessageHandler());
try
{
var result = await httpClient.PostAsJsonAsync("https://localhost:5001/Demo", new {});
}
catch (ProblemDetailsException problemDetailsException)
{
Console.WriteLine($"Title: {problemDetailsException.Details.Title}");
Console.WriteLine($"Detail: {problemDetailsException.Details.Detail}");
Console.WriteLine($"Instance: {problemDetailsException.Details.Instance}");
Console.WriteLine($"Type: {problemDetailsException.Details.Type}");
Console.WriteLine($"Status: {problemDetailsException.Details.Status}");
if (problemDetailsException.Details.Type == "https://example.net/validation-error")
{
Console.WriteLine($"Validation Errors:");
var validationErrors = problemDetailsException.Details.ToValidationProblemDetails();
foreach (var invalidParam in validationErrors.InvalidParams)
{
Console.WriteLine($"{invalidParam.Name} - {invalidParam.Reason}");
}
}
Console.ReadKey();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment