Search code examples
c#dotnet-httpclientrestsharp

Translating RestSharp request to HttpClient request


I'm programmatically uploading files to a remote server. The files are moderately large and I'd like to present a progress report to my users so they can see something happening. I was able to implement the upload using Postman which helpfully translated the whole thing to RestSharp. But RestSharp does not provide any kind of progress tracking. I tried to implement the same functionality using HttpClient but it goes wrong somewhere the and server just throws a "400 - Bad Request" without telling exactly what is bad about it (its API documentation is also not for the faint of heart).

So, here's what Postman / RestSharp provide and which is working:

var client = new RestClient("https://opencast/ingest/addMediaPackage");
      client.Timeout = -1;
      var request = new RestRequest(Method.POST);
      request.AddHeader("Authorization", "Basic FooBarBaz=");
      request.AddParameter("creator", file.Creator);
      request.AddParameter("title", file.Title);
      request.AddParameter("flavor", "presentation/source");
      request.AddParameter("description", file.Description);
      try
      {
           request.AddFile("BODY", path);
           IRestResponse response = await client.ExecuteAsync(request);
           _logger.LogInformation($"Response after file upload: {response.StatusCode}");
           File.Delete(path);  
      }
      catch (Exception ex)
      {
           _logger.LogError(ex, "Exception when uploading files: {Message}", ex.Message);
      }

and here's what I tried to do with HttpClient (without try-catch):

var request = new HttpRequestMessage(HttpMethod.Post, $"https://opencast/ingest/addMediaPackage");
request.Headers.Add("Authorization", "Basic FooBarBaz=");
using var form = new MultipartFormDataContent();
using var fileContent = new ByteArrayContent(await File.ReadAllBytesAsync(path));
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
form.Add(fileContent, "BODY", Path.GetFileName(path));
form.Add(new StringContent(file.Creator), "creator");
form.Add(new StringContent(file.Title), "title");
form.Add(new StringContent("presentation/source"), "flavor");
form.Add(new StringContent(file.Description), "description");
request.Content = form;
var client = clientFactory.CreateClient(); //which is a IHttpClientFactory
var response = await client.SendAsync(request);

This code sends the file to the server which, after completing the upload, throws a 400. Currently not seeing the difference. I could intercept the requests to see where they differ but maybe someone here can see the problem right away?

Update: It gets weirder. If I just use clientFactory.PostAsync(form) and add the Auth headers through form.Add then I get a 200 (i.e. Success) but the server simply swallows the file.


Solution

  • Okay, I found the solution. I'm not sure whether the WTF is me or the guys behind the server but...

    ... you need to add the fileContent last.

    Yes, the order of the parameters matters for this.