Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)

when i am trying to use my NodeJS api from the react app(building a MERN stack app) i get the error mentioned in question "Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)"

the api is working fine from postman

const onSubmit=async(e)=>{
    e.preventDefault()
    if(password!==password2){
        console.log('passwords dont match')
    }else{
        const newUser={
            name:name,
            email:email,
            password:password
        }
        try {
            const config={
                headers:{
                    'Content-Type':'application/json'
                }
            }
            const body=JSON.stringify(newUser)

            //axios has been set up as proxy
            //http://localhost:3000
            //we dont need to add the above to url
            const res =await axios.post('/api/users',body,config)
            console.log(res.data)

        } catch (error) {
            console.error(error.response.data)
        }
    }
}
like image 247
Swagath Shetty Avatar asked Dec 11 '25 01:12

Swagath Shetty


1 Answers

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. 431 can be used when the total size of request headers is too large, or when a single header field is too large. To help those running into this error, indicate which of the > two is the problem in the response body — ideally, also include which headers are too large. This lets users attempt to fix the problem, such as by clearing their cookies. Servers will often produce this status if: The Referer URL is too long There are too many Cookies sent in the request

In my case I was sending too many cookies because localhost:4200 was used as the domain of 3 different projects ... The solution (delete useless cookies)

Hope this helps...

like image 141
Yadiana Molina Avatar answered Dec 12 '25 15:12

Yadiana Molina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!