最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Multipart form post only works if you look at it first - Stack Overflow

programmeradmin2浏览0评论

I'm calling a rest endpoint that accepts a multi-part form data request. One of the fields is a file and I'm using a 42k test png image.

I'm using the HttpClient to make the request like so:

using var content = new MultipartFormDataContent()
{
    { new StringContent(message.SubscriberId.ToString()), "subscriberId" },
    { new StringContent(message.TemplateName), "templateName" },
};

if (message.Attachment != null)
{
    var c = new ByteArrayContent(message.Attachment.Data);
    content.Add(c, "file", message.Attachment.Filename);
}

using var request = new HttpRequestMessage(HttpMethod.Post, "transactions")
{
    Content = content
};

//await request.Content.LoadIntoBufferAsync();

using var msg = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

If I leave the commented out line as commented, I get a 500 internal server error back. If I uncomment that line, it works. Doing other things that spy on the request, such as calling ReadAsStringAsync() to log the request also make it work.

I've also discovered only sending the first 12200 bytes also makes it work, but this limit is not arbitrary - some images smaller than this don't work either & I have at least one 14k image that does work without the commented out line.

The fact that some images work some don't would suggest a fault in the REST API endpoint. But the fact I can make it work by looking at the request before I send it, suggests it's not entirely that.

I'm calling a rest endpoint that accepts a multi-part form data request. One of the fields is a file and I'm using a 42k test png image.

I'm using the HttpClient to make the request like so:

using var content = new MultipartFormDataContent()
{
    { new StringContent(message.SubscriberId.ToString()), "subscriberId" },
    { new StringContent(message.TemplateName), "templateName" },
};

if (message.Attachment != null)
{
    var c = new ByteArrayContent(message.Attachment.Data);
    content.Add(c, "file", message.Attachment.Filename);
}

using var request = new HttpRequestMessage(HttpMethod.Post, "transactions")
{
    Content = content
};

//await request.Content.LoadIntoBufferAsync();

using var msg = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

If I leave the commented out line as commented, I get a 500 internal server error back. If I uncomment that line, it works. Doing other things that spy on the request, such as calling ReadAsStringAsync() to log the request also make it work.

I've also discovered only sending the first 12200 bytes also makes it work, but this limit is not arbitrary - some images smaller than this don't work either & I have at least one 14k image that does work without the commented out line.

The fact that some images work some don't would suggest a fault in the REST API endpoint. But the fact I can make it work by looking at the request before I send it, suggests it's not entirely that.

Share asked Mar 14 at 13:47 Simon HalseySimon Halsey 5,4651 gold badge23 silver badges32 bronze badges 1
  • It looks like it's a bug in dotnet. When the code runs under dotnet framework 4.8, it works as expected. On dotnet 8, it fails – Simon Halsey Commented Mar 14 at 16:19
Add a comment  | 

1 Answer 1

Reset to default 0

If the content is not examined before being sent, or the LoadIntoBufferAsync method is not called, the content-length header is not set. This can cause an issue in some frameworks on the receiving end, such as Node, triggering an Unexpected end of form error.

The behaviour is also a regression from dotnet framework 4.8, which always sends the content-length header

If you encounter such an error, ensure you call LoadIntoBufferAsync before sending the request

发布评论

评论列表(0)

  1. 暂无评论