Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created November 3, 2021 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/420ffc05bd347d4447b1291d0486e542 to your computer and use it in GitHub Desktop.
Save dcomartin/420ffc05bd347d4447b1291d0486e542 to your computer and use it in GitHub Desktop.
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