Created
May 27, 2021 15:04
-
-
Save IEvangelist/651e8d0a1b3035c2cfbba1e735adc8c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the .NET fiddle with this running - enjoy 🎉