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:
Configuration Details:
var configuration = NSUrlSessionConfiguration.DefaultSessionConfiguration;
configuration.AllowsCellularAccess = true;
configuration.AllowsConstrainedNetworkAccess = true;
configuration.AllowsExpensiveNetworkAccess = true;
var session = new NSUrlSessionHandler(configuration);
var client = new HttpClient(session);
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}");
}
}
<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:
Questions:
Any insights or suggestions would be greatly appreciated!
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.
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.
We found these helpful resources that guided us through solving the issue: