DrivenTooFar
DrivenTooFar

Reputation: 110

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

Upvotes: 6

Views: 36582

Answers (3)

Daboul
Daboul

Reputation: 2743

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;

Upvotes: 4

Nilesh Sawant
Nilesh Sawant

Reputation: 1712

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>

Upvotes: 8

geneowak
geneowak

Reputation: 1318

I think the problem is on the server. The server is terminating the request because it exceeds it's configured maximum allowable request size.

Which server are you using? For Nginx users, the directive which determines what the allowable HTTP request size can be is client_max_body_size, the default maximum allowable request size is 1MB. The limit in Apache is set via the LimitRequestBody directive and defaults to 0 (meaning unlimited) to 2147483647 (2GB).

Check out this article on how you can fix it if you are using any of those two servers.

Upvotes: 0

Related Questions