Search code examples
c#iosxamarinfile-uploadhttpclient

Issues with Uploading Large Files using HttpClient in MAUI App – Getting Bad Request (400)


I'm facing an issue with uploading large files (over several MB) using HttpClient in a MAUI app. While the same server and client code work perfectly in other environments, I'm encountering a 400 Bad Request error when sending larger files from the MAUI app running in a simulator or on an iOS device.

What I've Tried:

  1. Working Scenarios:
  • Windows Console App: Successfully uploads large files.
  • Swift App on Mac: Successfully uploads large files.
  • Postman: Successfully uploads large files to the server.
  1. Problematic Scenarios:
  • MAUI Simulator: Uploading files larger than approximately 700 bytes fails with a 400 Bad Request.
  • Xamarin iOS App: Similar issue with 400 Bad Request for larger files.

Configuration Details:

  • HttpClient Initialization:
var configuration = NSUrlSessionConfiguration.DefaultSessionConfiguration;
configuration.AllowsCellularAccess = true;
configuration.AllowsConstrainedNetworkAccess = true;
configuration.AllowsExpensiveNetworkAccess = true;

var session = new NSUrlSessionHandler(configuration);
var client = new HttpClient(session);
  • Multipart Form Upload Example:
public async Task UploadFileAsync(string url, string filePath)
{
    using var client = new HttpClient();
    using var content = new MultipartFormDataContent();
    
    var fileContent = new StreamContent(File.OpenRead(filePath));
    fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    content.Add(fileContent, "file", Path.GetFileName(filePath));
    
    var response = await client.PostAsync(url, content);
    
    if (response.IsSuccessStatusCode)
    {
        Console.WriteLine("Upload successful!");
    }
    else
    {
        Console.WriteLine($"Error: {response.StatusCode}");
    }
}
  • App Transport Security Configuration:
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>mycompany.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.3</string>
        </dict>
    </dict>
</dict>

Troubleshooting Steps:

  • Configuration Check: Verified that NSUrlSessionConfiguration settings are appropriate for large file uploads.
  • File Size Test: Successfully uploading small files but encountering issues with files larger than approximately 1MB.

Questions:

  1. What could be causing the 400 Bad Request error specifically for larger files when using HttpClient in a MAUI app?
  2. Are there specific configurations or settings in NSUrlSessionHandler or NSUrlSessionConfiguration that I should adjust for handling large file uploads?
  3. Could there be any other environmental factors or simulator-specific issues that could be impacting the ability to upload larger files?

Any insights or suggestions would be greatly appreciated!


Solution

  • We encountered an issue where file uploads larger than 1MB were causing a Bad Request 400 error in our Spring Boot application. After some investigation, we found the solution and would like to share it here.

    Solution:

    The issue occurred because the default maximum file size in Spring Boot is set to 1MB when not explicitly configured in the application.properties file. This is controlled by Spring Boot’s multipart properties.

    The relevant configuration properties are:

    • spring.servlet.multipart.max-file-size: Specifies the maximum allowed size for uploaded files. The default is 1MB.
    • spring.servlet.multipart.max-request-size: Specifies the maximum allowed size for multipart/form-data requests. The default is 10MB.
    • spring.servlet.multipart.file-size-threshold: Specifies the size threshold after which files will be written to disk. The default is 0 (write to disk immediately).

    In our case, we updated the max-file-size and max-request-size in application.properties to allow larger file uploads:

    spring.servlet.multipart.max-file-size=50MB
    spring.servlet.multipart.max-request-size=50MB
    

    Additionally, we implemented a check to reduce the size of images if they exceed 2MB by resizing them to a maximum of 800px in width or height, and compressing their quality to 50%. This ensures that even if the file limit is increased, images are still optimized before uploading.

    Reference:

    We found these helpful resources that guided us through solving the issue: