Arti
Arti

Reputation: 3071

Voicebase API in c#

I am working on how to upload a media file using the VoiceBase API. I tried using HttpClient ( with MultipartFormDataContent and FormUrlEncodedContent), RestClient , WebClient and WebRequest. But it didn't worked.

Following is the code I tried:

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxx");
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new StringContent("http:/xx.mp3"), "media", "testFile.mp3");
HttpResponseMessage response = await client.PostAsync("https://apis.voicebase.com/v2-beta/media", form);
HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
   var a = reader.ReadToEndAsync();
   return response;
}

API returns this error:

"status": 400,
"errors": 
  {
    "error": "We have identified 1 error in your request: (1) Your upload was rejected because the media file not be successfully parsed (80 bytes )."
  },
"reference": "3D00BCA8:A910_0A40E32A:01BB_5949122A_69009:79C6"
}

Edit

I have also tried with binary data:

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxx");
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(File.ReadAllBytes(@"C:\Users\xxxx.mp3")), "media", "xxxx.mp3");
HttpResponseMessage response = await client.PostAsync("https://apis.voicebase.com/v2-beta/media", form);
HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
    var a = reader.ReadToEndAsync();
    return response;
}

With binary data I was getting following error:

{
"status": 400,
"errors": 
{
"error": "We have identified 1 error in your request: (1) We could not download the URL"
}
,
"reference": "3D00BCA8:DFF5_0A40E32A:01BB_59491448_6F33E:79C6"
}

Upvotes: 1

Views: 235

Answers (4)

cerebrotecnologico
cerebrotecnologico

Reputation: 247

If you use V3, you can generate the client code with Swagger on the language of your preference https://voicebase.readthedocs.io/en/v3/how-to-guides/swagger-codegen.html

Upvotes: 0

ianrathbone
ianrathbone

Reputation: 2362

I needed to do this today so I've made an attempt to wrap up my work on the V3 API in a nuget package here: https://www.nuget.org/packages/VoiceBaseV3/

When I get the time I'll get it on GitHub too

Upvotes: 1

JJJamie
JJJamie

Reputation: 109

Try this:

var content = new StreamContent(new FileStream(@"C:\Users\xxxx.mp3", FileMode.Open));
content.Headers.ContentType = new MediaTypeHeaderValue("audio/mp3");

MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(content, "media", "xxxx.mp3");

Upvotes: 0

Joe Phillips
Joe Phillips

Reputation: 51120

The first parameter of your form.Add(...) needs to be a data stream of the contents of the actual file, not a string. To do this you can make a new memory stream like this: new MemoryStream(File.ReadAllBytes(localfilePath))

Upvotes: 0

Related Questions