I am trying to use the sonicAPI file / load the API in C #.
My attempt to translate the curl example to C # with HttpClientand is MultipartFormDataContentreturning a 400 / Bad Request error.
Answer content:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status code="400" />
<errors>
<error message="File upload failed: file is missing." parameter="file" error_code="10" />
</errors>
</response>
An example curl command line shown in the documentation:
curl https://api.sonicapi.com/file/upload?access_id=$ACCESS_ID -Ffile=@Vocals.mp3
The code I have created so far:
public async Task<HttpResponseMessage> Post(string id, string fileName)
{
string url = string.Format("http://api.sonicapi.com/file/upload?access_id={0}", id);
var stream = new FileStream(fileName, FileMode.Open);
var client = new HttpClient { Timeout = TimeSpan.FromMinutes(10) };
var content = new MultipartFormDataContent();
content.Add(new StreamContent(stream), "file");
HttpResponseMessage message = await client.PostAsync(url, content);
string s = await message.Content.ReadAsStringAsync();
return message;
}
I tried to remove "file"from content.Add(new StreamContent(stream), "file");, but that did not help.
Note: loading occurs (i.e. it does not return immediately)
Do you know what is equivalent to the curl -F option when using .NET web classes?
EDIT:
Curl output -v
* Hostname was NOT found in DNS cache
* Trying 87.106.252.119...
* Connected to api.sonicapi.com (87.106.252.119) port 80 (
> POST /file/upload?access_id=xxxxxxxxxxxx HTTP/1.1
> User-Agent: curl/7.35.0
> Host: api.sonicapi.com
> Accept: */*
> Content-Length: 882266
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------b3c6dc0fc9
34fc71
>
< HTTP/1.1 100 Continue
< HTTP/1.1 201 Created
* Server nginx/0.7.67 is not blacklisted
< Server: nginx/0.7.67
< Date: Tue, 18 Feb 2014 21:14:09 GMT
< Content-Type: application/xml
< Connection: keep-alive
< X-Powered-By: Express
< X-Sonicapi-Request-Id: 6422cd9a-6069-4c2f-a3c5-0865c8ada6d5
< Access-Control-Allow-Origin: *
< location: /file/download?file_id=dae4e051-fe11-4058-a009-855dbb74de50
< X-Sonicapi-File-Id: dae4e051-fe11-4058-a009-855dbb74de50
< Content-Length: 249
<
<?xml version="1.0" encoding="utf-8"?><response><status code="201"/><file file_i
d="dae4e051-fe11-4058-a009-855dbb74de50" status="ready" href="/file/download?fil
e_id=dae4e051-fe11-4058-a009-855dbb74de50" remaining_lifetime_seconds="3599"/></
response>* Connection
Query output using Fiddly:
POST http://api.sonicapi.com/file/upload?access_id=xxxxxxxx
HTTP/1.1
Content-Type: multipart/form-data; boundary="bd6fba7f-c173-4470-9c44-c9cc91f618a9"
Host: api.sonicapi.com
Content-Length: 882175
Expect: 100-continue
Connection: Keep-Alive
--bd6fba7f-c173-4470-9c44-c9cc91f618a9
Content-Disposition: form-data; name=file
RIFFvu
WAVEfmt
(truncated)