问题描述
我正在 youtube 上完成使用 Redux 的 MERN 注册/登录身份验证教程.在 Postman 中尝试将测试用户 POST 到服务器时,我收到 431 标头请求太大错误响应.
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.
我在某些地方读到清除浏览器中的缓存/历史记录是有效的,所以我试过没有用.我还在标题请求中添加了一个 "Clear-Site-Data": "*" 条目(除了 "Content-Type": "application/json"),但它也不起作用.
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.
用于注册的客户端代码
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
});
});
};
用户注册应该向我连接的 Mongo 数据库发送姓名、电子邮件和密码,但是,它阻止了我并且 redux 命中我创建的 REGISTER_FAIL 类型,返回 431 错误.任何帮助将不胜感激.谢谢!
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!
推荐答案
我在我的 Angular 应用程序中遇到了同样的问题.花了很多时间后,我发现问题与Node JS有关.我们使用的是 Node JS v12.x.x,在这个版本中,max-http-header-size 从 80KB 减少到 8KB.我拥有的身份验证令牌大约为 10KB.这就是为什么当我重新加载应用程序时,浏览器开始为某些文件提供431 请求标头字段太大"错误.我已经更新了 Node JS v14.x.x,它再次开始工作,因为在 v14.0.0 中,max-http-header-size 已增加到 16KB.
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.
希望能帮到你.
这篇关于如何修复 React-Redux 应用程序中 431 请求头字段太大的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!