19

I'm working through a MERN sign up/login auth tutorial on youtube that uses Redux. When attempting to POST a test user to the server in Postman, I receive the 431 header request is too large error response.

I've read in some places that clearing the cache/history in your browser works, so I've tried that to no avail. I've also added in a "Clear-Site-Data": "*" entry to the header request (in addition to "Content-Type": "application/json") which hasn't worked, either.

Client Side Code for Sign Up

  onSubmit = e => {
    e.preventDefault();
    const { name, email, password } = this.state;

    const newUser = {
      name,
      email,
      password
    };

    this.props.register(newUser);
  };

//redux actions
export const register = ({ name, email, password }) => dispatch => {

  const config = {
    headers: {
      "Content-Type": "application/json",
      "Clear-Site-Data": "*"
    }
  };

  // Request body
  const body = JSON.stringify({ name, email, password });

  axios
    .post('/api/users', body, config)
    .then(res =>
      dispatch({
        type: REGISTER_SUCCESS,
        payload: res.data
      })
    )
    .catch(err => {
      dispatch(
        returnErrors(err.response.data, err.response.status, 'REGISTER_FAIL')
      );
      dispatch({
        type: REGISTER_FAIL
      });
    });
};

The user sign up should be sending a name, email and password to my connected Mongo db, however, it halts me and redux hits the REGISTER_FAIL type I created returning the 431 error. Any help would be greatly appreciated. Thank you!

Brent Abruzese
  • 191
  • 1
  • 1
  • 6
  • What is the server that you are using? Is your express server sitting behind an nginx? What express middleware are you using? Also, providing the actual HTTP request would be pretty useful. – swazza85 Aug 10 '19 at 18:27

11 Answers11

19

I had faced the same issue in my Angular Application. After spending a lot of time, I had found out that the issue is related with Node JS. We were using Node JS v12.x.x, and in this version, max-http-header-size reduced to 8KB from 80KB. And the auth token which I had was of around 10KB. That's why, when I reload the app, browser starts giving '431 request header fields too large' error for some of the files. I had updated the Node JS v14.x.x and it starts working again because in v14.0.0, max-http-header-size has been increased to 16KB.

Hope it will be helpful.

Gourav Kajal
  • 201
  • 2
  • 3
  • The site would work for me in incognito mode so I figured this solution might work and it did – dwilt Feb 17 '22 at 15:26
10

Another suggestion would be to access your cookies, in the inspector tool, and delete. applicable cookies for your localhost:{port} application.

JhWebDev
  • 332
  • 6
  • 13
8

I had similar problems with just using localhost(not limited to redux). Maybe this might help.
Put this into url: chrome://settings/?search=cache
Click on Clear Browsing data.
Tick cookies and other site data (Important since cookies is in HTTP header) TIck cached images and files (might be optional)

hansss
  • 191
  • 3
  • 4
4

Not reactjs, but using vue-cli, for people like me, just being stupid it may help:

I started my Vue app on port 8080, and my local backend was running at port 4000. However my requests pointed to 8080 and the response I got from Webpack Serving was "431 Request Header Fields Too Large".

The plain solution was just to use the right backend-port. Even though that was a really stupid mistake of me, the error message is kinda useless here.

Karl Adler
  • 14,019
  • 10
  • 62
  • 86
1

It means you are trying to do this fetch on your current front-end development server. You need to specifiy the server address. For example:

.post('/api/users', body, config)

should read

.post('http://localhost:4000/api/users', body, config)

Another fix would be to change the line proxy in your package.json from localhost:3000 to localhost:4000 assuming that 4000 is your actual server port.

Richard Bonneau
  • 883
  • 8
  • 8
  • 1
    The Proxy solution is what worked for me; my dataProvider.js in React was accessing my back-end port directly without the proxy, generating this 431 error. Changing it to access '/api' instead of 'localhost:3000/api', and putting the proxy into the front-end package.json did the trick. Thanks! – Jeff Breadner Mar 03 '21 at 21:31
1

Is it from Brad Travery's course? Check "proxy" in package.json, or try using full url in axios request. I had to completely restart server after changes, bc it's still use the old port (btw, I was enter wrong port)

Toan Ka
  • 51
  • 1
  • 3
1

In my react app, I add --max_old_space_size flag and it is worked. Current start script is :

"start": "react-scripts --expose-gc --max_old_space_size=12000 start",
cansu
  • 994
  • 7
  • 20
0

The issue I was having is that I was trying to access a file in the src directory. The fix is to move it to the public directory and it now works fine.

E.g. From

public
 - index.html
 - favicon.ico
 - etc
src
 > access-me
 - App.tsx
 - etc

to

public
 > access-me
 - index.html
 - favicon.ico
 - etc
src
 - App.tsx
 - etc
Nate Levin
  • 741
  • 7
  • 19
0

Fixed in https://stackoverflow.com/a/56351573/9285308

(the max http header size parameter is configurable):

node --max-http-header-size 16000 client.js
0

Just find a solution,

NETCORE 6.0 / React template VS 2022

you have to setup the proxy url in package.json with the value of your url asp net application ! AspNet URL in debug console

So you can have that 431 error when you use the proxy of default React/AspNetCore project and you don't setup a proxy url (or a valid one) in the package.json.

proxy url in package.json

0

Just change your start script in package.json file and you are good to go.

"start": "react-scripts --max-http-header-size=1024 start",
Muhammad Muzamil
  • 751
  • 1
  • 12
  • 21