-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C# Client Posting Multipart/Form-Data To C# API Endpoint Throws InvalidDataException #10503
Comments
cc @Tratcher |
Notes: I'm pretty sure these parts are unnecessary:
Those leading dashes are not logically part of the boundary value. |
Yes, it's your boundary that's the problem. |
I needed the boundary workaround:
otherwise, the file won't make it to the endpoint. As you can see in the newly attached failed request, the boundary in the Content-Type is set as
failed-csharp-request-no-boundary-workaround.txt |
You're solving the wrong problem. Look at your postman example. The boundary field is The real question here is why do you get How about removing IFormFileCollection as the method parameter and calling HttpContext.Request.ReadFormAsync() directly? Then inspect the resulting form to see how it was parsed. |
Updating the endpoint to:
did in fact work successfully when called via the C# client. However, removing the Can you please provide more detail into why the |
The examples I've seen used |
FWIW: Switching from |
@ideoclickVanessa I tried this with our most recent 3.0 builds and it works just fine. We fixed a couple of form file related issues in 3.0, perhaps that could be it. The other possible reason could be that model binding requires that the name of all of the foreach (IFormFile file in files)
{
StreamContent streamContent = new StreamContent(file.OpenReadStream());
requestContent.Add(streamContent, file.Name, file.FileName);
streamContent.Headers.ContentDisposition.FileNameStar = "";
} Regardless, closing this since this issue seems to be resolved with the most recent builds. |
@pranavkm If that is indeed what you are saying, why does the endpoint work successfully from Postman when using differently named files? Also, could you update my foreach statement to reflect the changes that are needed so that I can better understand what you're trying to tell me? |
Yup. By the way, these are names of the form file in the request, not the actual file names on disk or the file names in the content-disposition. foreach (IFormFile file in files)
{
StreamContent streamContent = new StreamContent(file.OpenReadStream());
requestContent.Add(streamContent, name: "files", fileName: file.FileName);
} |
Ah.... ok. That makes much more sense. Thanks :) |
Describe the bug
I have a C# API endpoint that accepts files for processing, defined like so:
When I call this endpoint from Postman, the endpoint successfully receives / processes the files.
However, I receive an
InvalidDataException: Multipart body length limit 16384 exceeded.
exception when I call the endpoint from a C# client using this code:Google / Stack Overflow suggest that the exception should be resolved by using either the attribute
[RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
or configured globally in Startup:However, neither suggested solution prevents the
InvalidDataException
from being thrown when the endpoint is called by the C# client.Can you please provide guidance as to whether this is a bug in the framework or my C# client code?
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The C# client successfully posts the request without triggering the
InvalidDataException
.Additional context
dotnet-info.txt
testfile.jpg
successful-postman-request.txt
failed-csharp-request.txt
The text was updated successfully, but these errors were encountered: