Why Microsoft Cognitive Speech Api is sending "Bad Request" even with all the requirements fulfilled

86 Views Asked by At

I have been trying to implement Microsoft Cognitive Speech-to-text API to convert an audio file to text but always I am getting a Bad Request message.

Here is sample code I am implementing:

public static object MC()
{
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", speechKey);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("audio/wav"));

    var mp = new MultipartFormDataContent();
    var file = new StreamContent(File.OpenRead(HttpContext.Current.Server.MapPath("~/audios/audio_06012023094443961.wav")));
    file.Headers.ContentType = new MediaTypeHeaderValue("audio/wav");
    mp.Add(file, "audio", "audio_06012023094319156.wav");

    var resp = client.PostAsync("https://" + speechRegion + ".stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US", mp);
    while (!resp.IsCompleted)
    {
        Thread.Sleep(100);
    }
    return resp.Result.ReasonPhrase;
}

My programming language is C#.

0

There are 0 best solutions below