Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

413 request entity too large - Web API

I'm running into a 413 issue while trying to send data from my web application (.netfx 4.6.1) to my web api (.net core 3.1). In the code below, I send a list over containing byte data of images along with additional data needed to build a file. The expected output is to return a byte array containing the new file. Unfortunately when sending the request I receive an error: Response status code does not indicate success: 413 (Request Entity Too Large).

The error only seems to occur when the file is large to begin with, which makes sense. The research I've done seems to point to settings in IIS, the main ones being maxAllowedContentLength, maxRequestLength, and uploadReadAheadSize. I've tried increasing these values to ones more suited to this process but nothing seems to work. I've adjusted them for both the web application and the web api, as I was not sure which one was causing the problem.

Where does the problem lie? In the application, the API, or both? Is there an additional setting I'm missing to allow an increased size? Is there an issue with how I'm sending the request? Any help is appreciated.

    public static async Task<byte[]> CreatePdfFromImageFilesAsync(List<ImageFile> imageFiles)
    {
        var list = new List<dynamic>();
        foreach (var item in imageFiles)
        {
            list.Add(new
            {
                Data = Convert.ToBase64String(item.Bytes),
                PageOrder = item.PageOrder,
                Rotation = item.Rotation,
                Type = "PDF"
            });
        }

        var response = _client.PostAsJsonAsync($"{FileCreatorAPI}/api/files/CreateFileFromMultiple", list).Result;
        var result = response.EnsureSuccessStatusCode();
        var bytes = await result.Content.ReadAsAsync<byte[]>();
        return bytes;
    }
like image 392
DrivenTooFar Avatar asked Jan 17 '20 14:01

DrivenTooFar


2 Answers

Below changes worked for me

                // If using Kestrel:
                .Configure<KestrelServerOptions>(options =>
                {
                    options.AllowSynchronousIO = true;
                    //options.Limits.MaxRequestBodySize = null; --did not worked
                    options.Limits.MaxRequestBodySize = int.MaxValue;
                })
                // If using IIS:
                .Configure<IISServerOptions>(options =>
                {
                    options.AllowSynchronousIO = true;
                    //options.MaxRequestBodySize = null;
                    options.MaxRequestBodySize = int.MaxValue;
                });

create web.config file and add following configuration

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
like image 123
Nilesh Sawant Avatar answered Oct 23 '22 12:10

Nilesh Sawant


Can you check that attribute https://github.com/aspnet/Announcements/issues/267 ? Using

[RequestSizeLimit(100_000_000)] 

on your controller entry point, or more globally setting it this way:

.UseKestrel(options =>
{
    options.Limits.MaxRequestBodySize = null;

EDIT: article from MS: https://dotnet.microsoft.com/download/dotnet-core

like image 3
Daboul Avatar answered Oct 23 '22 11:10

Daboul