Skip to content

Instantly share code, notes, and snippets.

@IEvangelist
Created May 27, 2021 15:04
Show Gist options
  • Save IEvangelist/651e8d0a1b3035c2cfbba1e735adc8c2 to your computer and use it in GitHub Desktop.
Save IEvangelist/651e8d0a1b3035c2cfbba1e735adc8c2 to your computer and use it in GitHub Desktop.
#nullable enable
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
JsonSerializerOptions _options = new()
{
PropertyNameCaseInsensitive = true
};
using HttpClient httpClient = new();
// An array with a single joke is returned
Joke[]? jokes = await httpClient.GetFromJsonAsync<Joke[]>(
"https://official-joke-api.appspot.com/jokes/programming/random", _options);
Joke? joke = jokes?[0];
string text = joke is not null
? $"{joke.Setup}{Environment.NewLine}{joke.Punchline}"
: "No joke here...";
Console.WriteLine(text);
public record Joke(int Id, string Type, string Setup, string Punchline);
@IEvangelist
Copy link
Author

IEvangelist commented May 27, 2021

Here is the .NET fiddle with this running - enjoy 🎉

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment