gpt4 book ai didi

javascript - 如何在 express 和 bodyParser 中接受 application/csp-report 作为 json?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:13 26 4
gpt4 key购买 nike

我正在尝试编写一个中间件来接受来自浏览器的 CSP 报告。浏览器将 application/csp-report 作为 Content-Type 发布。发布的请求是 JSON 格式。目前我使用 bodyParser.text 来接受该内容类型。但我认为可能有更好的方法来接受 application/csp-report 作为 bodyParser 中的 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/

关于javascript - 如何在 express 和 bodyParser 中接受 application/csp-report 作为 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36165008/

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