我正在研究如何使用VoiceBase API上传媒体文件。我尝试使用HttpClient(包含MultipartFormDataContent和FormUrlEncodedContent),RestClient,WebClient和WebRequest。但它没有奏效。
以下是我尝试过的代码:
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返回此错误:
"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"
}
修改
我也试过二进制数据:
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;
}
对于二进制数据,我遇到了以下错误:
{
"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"
}
答案 0 :(得分:1)
我今天需要这样做,所以我试图在nuget包中完成V3 API的工作: https://www.nuget.org/packages/VoiceBaseV3/
当我得到时间时,我也会在GitHub上得到它
答案 1 :(得分:0)
form.Add(...)
的第一个参数需要是实际文件内容的数据流,而不是字符串。为此,您可以创建如下的新内存流:new MemoryStream(File.ReadAllBytes(localfilePath))
答案 2 :(得分:0)
试试这个:
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");
答案 3 :(得分:0)
如果您使用的是V3,则可以使用Swagger根据您的偏好语言生成客户端代码 https://voicebase.readthedocs.io/en/v3/how-to-guides/swagger-codegen.html