using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
public record GenderResponse(
[property: JsonPropertyName("first_name")] string FirstName,
[property: JsonPropertyName("gender")] string Gender,
[property: JsonPropertyName("probability")] double Probability
);
public class Program
{
public static async Task Main()
{
var apiKey = "YOUR_API_KEY";
using var client = new HttpClient();
var requestBody = new { first_name = "Theresa" };
var jsonContent = new StringContent(
JsonSerializer.Serialize(requestBody),
Encoding.UTF8,
"application/json");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
try
{
var response = await client.PostAsync("https://gender-api.com/v2/gender/by-first-name", jsonContent);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<GenderResponse>(jsonResponse);
Console.WriteLine($"Gender: {result.Gender}");
Console.WriteLine($"Probability: {result.Probability}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}