Search code examples
c#asp.net.net-coredotnet-httpclienthockeyapp

CURL form post (-F) to HttpClient post


I need to upload an apk to hockeyApp with c#'s HttpClient,

the cUrl to upload an apk is the following:

curl \
  -F "status=2" \ 
  -F "notify=1" \ 
  -F "[email protected]" \ 
  -H "X-HockeyAppToken: 4567abcd8901ef234567abcd8901ef23" \
   https://rink.hockeyapp.net/api/2/apps/upload

i tried to do the same with c#:

var stream = await File.ReadAllBytesAsync(apkFilePath);
var bytes = new ByteArrayContent(stream);
bytes.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var multipartFormDataContent = new MultipartFormDataContent
{
    //send form text values here
    {new StringContent("2"), "status"},
    {new StringContent("0"), "notify"},
    // send file Here
    {bytes, "ipa"}
};

var uri = "https://rink.hockeyapp.net/api/2/apps/upload";

multipartFormDataContent.Headers.Add("X-HockeyAppToken", "++token_here++");

var response = await _client.PostAsync(uri, multipartFormDataContent);

but the response i am getting (after a long period) is 422 unprocessable entity


Solution

  • Solved,

    the problem was in the boundary

    the cUrl command produces a boundary in this form boundary=xxxxxxxxxxx (no quotes)

    but the multipartFormDataContent has a boundary in this form boundary="xxxxxxxxxxx" (with quotes)

    when i removed the quotes it worked fine:

    // Fix boundary problem (boundary is quoted)
    var boundary = multipartFormDataContent.Headers.ContentType.Parameters.First(o => o.Name == "boundary");
    boundary.Value = boundary.Value.Replace("\"", string.Empty);