runtime: HttpClient on Android sends wrong content-type header with ByteArrayContent

Description

In theory, the content-type header should be automatically set based on the type used to set the Content property of an http request. As a result, the content-type for a ByteArrayContent object should match up to some binary content type value.

      var client = new HttpClient();
      var content = new ByteArrayContent(serialized);
      var httpResponse = await client.PostAsync(
        "<server url>",
        content);

In practice this is what happens:

Platform Behavior
Windows content-type is not set at all
Android content-type is set to application/x-www-form-urlencoded

The Windows behavior, while incorrect, at least works.

The Android behavior doesn’t work at all, because the aspnetcore server doesn’t properly take the binary payload and make it available in Request.Body like it should.

The result is that we can’t post binary data from a MAUI app on Android to aspnetcore. Accounts of this exist on stackoverflow and in CSLA.

Steps to Reproduce

  1. Create an Android MAUI app and a Console app with code like this:
      var content = new ByteArrayContent(serialized);
      var httpResponse = await client.PostAsync(
        "<server url>",
        content);
  1. Create an ASP.NET Core web site with a controller like this:
    [HttpPost]
    public virtual async Task PostAsync([FromQuery] string operation)
    {
        byte[] data;
        using (var requestBodyBuffer = new MemoryStream())
        {
          await Request.Body.CopyToAsync(requestBodyBuffer).ConfigureAwait(false);
          Request.Body.Position = 0;
          data = requestBodyBuffer.ToArray();
        }
    }
  1. Run the web server
  2. Run the console app and it works - data has data
  3. Run the Android app and it fails - data is empty
  4. Use something like ngrok to see what’s going over the wire - the exact same binary payload goes over the wire in both cases; the http content-type value is set wrong from Android and not at all from Windows

Link to public reproduction project repository

No response

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

The workaround is to base64 encode the binary data and send it to the server using a StringContent object instead of the broken ByteArrayContent type.

Obviously the server endpoint has to accept and decode the string from Android and the binary payload from all other platforms.

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 17 (8 by maintainers)

Most upvoted comments

@simonrozsival I did the sanity check and it worked. So I switched back to my server and it worked. Clearly I messed something up in my original test run 😳

So I think my takeaway from this whole thing is that the default content type for ByteArrayContent is random, and (imo) the HttpClient API (or an analyzer) should fail the build if someone writes code that doesn’t explicitly set the content-type for the content.

Sure, it’ll work in Windows and Linux, so most people never encounter this issue. But realistically, something is broken here imo. Docs, API, analyzers. People shouldn’t fall into a hole where bing/google can’t help and only @dotMorten can save the day (as cool as he is!).