2

When my React app runs a post request I get the "431 (Request Header Fields Too Large)" error and I can't figure out what's causing it. Can anyone help?

Things I've tried based on answers to similar questions:

  • Clearing cache and cookies
  • Updating node from v12.19.0 to v14.17.2
  • Setting header specs in the request

Function from form submitting the POST request:

handleSubmit(evt){
    axios.post('/creator/new', {
        username: this.state.username,
        password: this.state.password,
        profilePic: this.state.profilePic,
        firstName: this.state.firstName,
        lastName: this.state.lastName,
        dob: this.state.dob,
        country: this.state.country
    }, {headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=UTF-8'
    }}).then(response => {
        console.log('response received');
    })
    evt.target.reset();
    this.props.history.push(`/creator/${this.state.username}`);
    this.props.addCreator(this.state);
}

Server:

const express = require('express');
const cors = require('cors');

const creatorsRoutes = require('./routes/creators-routes');
const ipsumsRoutes = require('./routes/ipsums-routes');


const app = express();

app.use(cors());
app.use(express.json());

app.use((req, res, next) => {
  console.log('request received')
  res.send(req.path);
  console.log('request body sent back')
})

app.use('/creator', creatorsRoutes); 


const mongoose = require('mongoose');
mongoose.connect("mongodb+srv://SkyeWulff:************retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => {
      console.log('Connection open');
    })
    .catch(err => {
      console.log('Error occured:');
      console.log(err);
    })
let db = mongoose.connection;

db.on('error', console.error.bind(console, 'Connection Error'));

db.once('open', function(){console.log("successfully connected to MongoDB")});

app.listen(5000, () => {
  console.log("listening on port 5000");
})

Router the server is using:

const express = require('express');
const creatorsController = require('../controllers/creators-controllers');

const router = express.Router();


router.get('/creator/id', creatorsController.getCreator);


router.post('/new', creatorsController.addCreator);

module.exports = router;

Controller used by router:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const creatorSchema = new Schema({
    username: String,
    password: String,
    profilePic: String,
    firstName: String,
    lastName: String,
    dob: Date,
    country: String
});
module.exports = mongoose.model('Creator', creatorSchema);

Filetree Structure: Filetree

SkyeWulff
  • 41
  • 1
  • 5

1 Answers1

0

As mentioned in https://stackoverflow.com/a/68092880/4437911

(the max http header size parameter is configurable):
node --max-http-header-size 16000 client.js

barha
  • 709
  • 1
  • 7
  • 22
  • further reading about the limitation - https://cri.dev/posts/2020-05-18-Fixing-431-Request-Header-Fields-Too-Large-in-Nodejs/ – barha Jul 05 '21 at 22:01
  • 1
    Thanks barha. I tried this and unfortunately I'm still getting the error. I restarted the server and reran npm start to be sure that the change was reflected in localhost but it still showed the same error. I also tried changing 16000 to 25000 just to give more space and it still didn't work. – SkyeWulff Jul 06 '21 at 01:55