gpt4 book ai didi

rest - GET 请求中 `req.body` 始终为空

转载 作者:行者123 更新时间:2023-12-02 08:16:40 25 4
gpt4 key购买 nike

我正在使用 Express 4.13.3 和我的 req.body在 GET 请求中始终为空。它在 POST 请求中填充了正确的数据。为什么是这样?我在 Express 文档中找不到对这种差异的任何引用。

我的 express 配置:

function onError(err, req, res, next) { // eslint-disable-line no-unused-vars
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500; // eslint-disable-line no-param-reassign
res.end(`${res.sentry}\n`);
}

const render = require('../public/assets/SSR');

const app = express();

// sentry.io
app.use(raven.middleware.express.requestHandler(process.env.SENTRY_DSN));

const db = connectDb();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(cookieParser());

if (process.env.NODE_ENV === 'dev') {
// Hot reloading using existing express server
const compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
}));
app.use(webpackHotMiddleware(compiler));

app.use(logger('dev'));
}

if (process.env.NODE_ENV === 'prod') {
app.use(helmet());
}

configPassport(app, passport, db);
configRoutes(app, passport, db);
app.use(ua.middleware(process.env.GA_TRACKING_ID, { cookieName: '_ga' }));

// sentry.io
// The error handler must be before any other error middleware
app.use(raven.middleware.express.errorHandler(process.env.SENTRY_DSN));

// Optional fallthrough error handler
app.use(onError);

app.get('*', render.default);

const port = process.env.PORT || 3000;
app.listen(port);
console.log(`Listening on port ${port}`);
console.log(`You are working in ${process.env.NODE_ENV} 😋`);

最佳答案

HTTP GET 请求的正文应为空。毫无意义。虽然理论上可以向 GET 请求添加请求正文,但许多客户端不这样做或拒绝,许多服务器将其剥离。

简而言之:这是故意的。如果您确实在 GET 中有一个请求正文,请将其删除或切换到更合适的 HTTP 方法。

关于rest - GET 请求中 `req.body` 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41351777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com