Skip to content

from curl to C# #11

@cyberprophet

Description

@cyberprophet

Sure! Here's an example of how you can convert the given cURL command into C# code using the HttpClient class:

using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        string apiKey = "{API Key}";
        string endpointUrl = "{API Endpoint URL}";

        string xmlData = @"<speak>
                                <voice name=""Summer"">안녕하세요. 반가워요.</voice>
                                <voice name=""Roman"">잘 지냈어요?</voice>
                            </speak>";

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("x-api-key", apiKey);
            client.DefaultRequestHeaders.Add("Content-Type", "application/xml");
            client.DefaultRequestHeaders.Add("X-TTS-Engine", "deep");

            var content = new StringContent(xmlData);

            try
            {
                HttpResponseMessage response = await client.PostAsync(endpointUrl, content);
                response.EnsureSuccessStatusCode();

                byte[] audioBytes = await response.Content.ReadAsByteArrayAsync();

                // Save the audio bytes to a file
                string filePath = "test.mp3";
                await System.IO.File.WriteAllBytesAsync(filePath, audioBytes);

                Console.WriteLine("Audio file saved successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}

Make sure to replace {API Key} with your actual API key and {API Endpoint URL} with the correct endpoint URL provided by the API service you're using.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions