I am trying to use the sonicAPI file/upload API in C#.

My attempt to translate the curl example to C# with HttpClient and MultipartFormDataContent returns the error 400 / Bad Request.

Content of the response :

<?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>

Example of the curl command-line shown in the documentation :

curl https://api.sonicapi.com/file/upload?access_id=$ACCESS_ID -Ffile=@Vocals.mp3

Code I've crafted 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've tried to remove "file" from content.Add(new StreamContent(stream), "file"); but it didn't help.

Note : the upload happens (i.e. it does not return immediately)

Do you know what is the equivalent of the curl -F parameter when using .NET web classes ?

EDIT :

Output of curl -v

* Hostname was NOT found in DNS cache
*   Trying 87.106.252.119...
* Connected to api.sonicapi.com (87.106.252.119) port 80 (#0)
> 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 #0 to host api.sonicapi.com left intact

Output of the request 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)

有帮助吗?

解决方案

Thanks to @bzlm, using Fiddler I managed to track what was missing :

  • Content disposition
  • Content type

And these needed to be set on streamContent rather than content.

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);
    string name = Path.GetFileName(fileName);

    var client = new HttpClient { Timeout = TimeSpan.FromMinutes(10) };

    var streamContent = new StreamContent(stream);
    streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
    streamContent.Headers.ContentDisposition.Name = "\"file\"";
    streamContent.Headers.ContentDisposition.FileName = "\"" + name + "\"";
    streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

    var content = new MultipartFormDataContent { streamContent };
    HttpResponseMessage message = await client.PostAsync(url, content);
    string s = await message.Content.ReadAsStringAsync();
    return message;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top