如何在 express 和 bodyParser 中接受 application/csp-report 作为 json?
How to accept application/csp-report as json in express and bodyParser?
我正在尝试编写一个中间件来接受来自浏览器的 CSP 报告。浏览器将 application/csp-report
发布为 Content-Type
。正在发布的请求是 JSON 格式。目前我使用 bodyParser.text
来接受该内容类型。但我认为可能有更好的方法在 bodyParser 中接受 application/csp-report
作为 JSON。
这就是我现在正在做的事情。
app.use(bodyParser.json());
app.use(bodyParser.text({type: 'application/csp-report'}));
我的问题是如何使用 Content-Type
application-csp-report
接受 JSON 请求负载?
因为它实际上是 JSON 你可以像这样通知 Express 这个事实:
app.use(bodyParser.json({type: 'application/csp-report'}));
请注意,有些浏览器使用 application/csp-report
,有些浏览器使用 application/json
,所以我同时设置了:
app.use(bodyParser.json({type: 'application/json'}));
app.use(bodyParser.json({type: 'application/csp-report'}));
如果有帮助,我在这里为(非常简单的)节点报告服务编写代码:https://www.tunetheweb.com/security/http-security-headers/csp/
除了@Barry 的回答,您还可以更具体地设置端点路径:
app.use('/report-violation', bodyParser.json({ type: 'application/json' }));
app.use('/report-violation', bodyParser.json({ type: 'application/csp-report' }));
app.use('/report-violation', (req, res) => {
// handle req.body
});
我正在尝试编写一个中间件来接受来自浏览器的 CSP 报告。浏览器将 application/csp-report
发布为 Content-Type
。正在发布的请求是 JSON 格式。目前我使用 bodyParser.text
来接受该内容类型。但我认为可能有更好的方法在 bodyParser 中接受 application/csp-report
作为 JSON。
这就是我现在正在做的事情。
app.use(bodyParser.json());
app.use(bodyParser.text({type: 'application/csp-report'}));
我的问题是如何使用 Content-Type
application-csp-report
接受 JSON 请求负载?
因为它实际上是 JSON 你可以像这样通知 Express 这个事实:
app.use(bodyParser.json({type: 'application/csp-report'}));
请注意,有些浏览器使用 application/csp-report
,有些浏览器使用 application/json
,所以我同时设置了:
app.use(bodyParser.json({type: 'application/json'}));
app.use(bodyParser.json({type: 'application/csp-report'}));
如果有帮助,我在这里为(非常简单的)节点报告服务编写代码:https://www.tunetheweb.com/security/http-security-headers/csp/
除了@Barry 的回答,您还可以更具体地设置端点路径:
app.use('/report-violation', bodyParser.json({ type: 'application/json' }));
app.use('/report-violation', bodyParser.json({ type: 'application/csp-report' }));
app.use('/report-violation', (req, res) => {
// handle req.body
});