musium
musium

Reputation: 3072

Https upload MultipartForm returns 401 unauthorized

I’m trying to upload a coverity scan result to coverity.

This is my code:

public static void Main( String[] args )
{
    var client = new HttpClient
    {
        Timeout = TimeSpan.FromMinutes( 20 )
    };
    var form = new MultipartFormDataContent
    {
        { new StringContent( "my tooken" ), "token" },
        { new StringContent( "my email" ), "email" },
        { new StringContent( "1.1.1.1" ), "version" },
        { new StringContent( "Test..." ), "description" }
    };

    var fs = new FileStream( @"cov-int.zip", FileMode.Open, FileAccess.Read );
    form.Add(new StreamContent(fs), "file", "cov-int.zip");

    var task = client.PostAsync("https://scan.coverity.com/builds?project=Name/Porject", form);
    try
    {
        task.Wait();
    }
    catch ( AggregateException ex )
    {
        throw ex.InnerException;
    }
    var result = task.Result;
    fs.Close();
}

The post always end with a failed status (401 unauthorized):

{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  x-xss-protection: 1; mode=block
  x-request-id: 70dfc119-7d78-47fe-86a7-8505d73225e4
  x-runtime: 1.675468
  x-frame-options: SAMEORIGIN
  x-content-type-options: nosniff
  Connection: close
  Status: 401 Unauthorized
  Transfer-Encoding: chunked
  Cache-Control: no-cache
  Date: Fri, 29 May 2015 13:01:06 GMT
  Server: Apache
  X-Powered-By: Phusion Passenger 5.0.8
  Content-Type: text/html; charset=utf-8
}}

I’ve tried to upload the same data, from the same machine to the same server using curl:

curl --form token="token" --form email="email" --form file="cov-int.zip" --form version="1.1.1.1" --form description="a message" --insecure https://scan.coverity.com/builds?project=Name/Project

Uploading the data with curl works.

What am I doing wrong in my C# code?

Upvotes: 0

Views: 1195

Answers (1)

csmacnz
csmacnz

Reputation: 351

Something possibly changed on Coverity's end, since this was working two weeks ago. Based on this other question here How to upload files to Asp.Net MVC 4.0 action running in IIS Express with HttpClient class included in .Net 4.0 I've found what I think is the solution.

You need to add extra quotes to your form-data names.

var form = new MultipartFormDataContent
{
    { new StringContent( "my tooken" ), "\"token\"" },
    { new StringContent( "my email" ), "\"email\"" },
    { new StringContent( "1.1.1.1" ), "\"version\"" },
    { new StringContent( "Test..." ), "\"description\"" }
};

form.Add(new StreamContent(fs), "\"file\"", "cov-int.zip");

Can't be 100% sure this works since I used up all my attempts for today and will have to wait until tomorrow to see a success response. But I am getting the "The build submission quota for this project has been reached." message and not the Access Denied message now.

Upvotes: 1

Related Questions