413 request entity too large - Web API

DrivenTooFar picture DrivenTooFar · Jan 17, 2020 · Viewed 10.8k times · Source

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;
    }

Answer

Daboul picture Daboul · Jan 17, 2020

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