Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core with React - 431 Request headers too long

I have a dotnet application that I start with the dotnet run command. I also have a React app, that I start with yarn start.

When I open the browser on localhost:3000 (where the react app is) the server log looks like this:

....this goes on for long
 Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Server.Kestrel[17]
      Connection id "0HLMFVTO65C53" bad request data: "Request headers too long."
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request headers too long.
   at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TakeMessageHeaders(ReadOnlySequence`1 buffer, SequencePosition& consumed, SequencePosition& examined)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(ReadOnlySequence`1 buffer, SequencePosition& consumed, SequencePosition& examined)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean& endConnection)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication`1 application)

After like 15 seconds of this the page loads, but I get an error in the browser console about the bundle.js 431 error.

If I make the RequestHeaders max total size larger for the Kestrel server, the same thing happens but this goes on for even longer and the end result is a 500 server error instead of 431.

Moreover if I try to make a simple DELETE request to the server using postman the result is pretty much the same. As if the request was stuck in an infinite loop and then returns a 431.

Lines from Startup.cs that might be relevant:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
     {
          configuration.RootPath = "client/build";
     }
);


app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            }
);
app.UseSpaStaticFiles();
app.UseSpa(spa =>
            {
                if (env.IsDevelopment())
                    spa.UseProxyToSpaDevelopmentServer("http://localhost:3000");
            }
);

What is going on?

like image 924
J. Loe Avatar asked May 03 '19 19:05

J. Loe


People also ask

How do I fix 431 request header fields too large?

Clear Your Cookies Too many cookies in the request can cause a web page to show the HTTP error 431 status instead of its content. Websites often use cookies to store preferences, give relevant content, and keep users signed in. However, clearing the browser cookies helps fix this issue.

How do I fix the request header size is too long?

The “Bad Request – Request Too Long” error is exclusive to browsers. The typical solution is to clear the cache and cookies in your browser.

What does 431 request header fields too large mean?

The HTTP 431 Request Header Fields Too Large response status code indicates that the server refuses to process the request because the request's HTTP headers are too long. The request may be resubmitted after reducing the size of the request headers.


1 Answers

I ran into the same issue, but with Angular, using asp.net core during localhost dev. The solution for me was to set the header size for node in the "start" script inside of my package.json file.

Inside scripts json object:

"start": "node --max-http-header-size=100000 ./node_modules/@angular/cli/bin/ng serve",
like image 81
Rafe Avatar answered Sep 21 '22 03:09

Rafe