我已经创建了一个 API 端点来处理来自客户端的 http POST 请求。 目前使用 Express 框架和 bodyParser 来处理请求主体。我如何使用 body-parser 来处理请求主体可能是 gzip 压缩的 Json 对象或纯 Json 对象的情况。
bodyParser json 解析器只会处理请求头中的 Content-Type 和下面的类型都相同的请求。
bodyParser.json({type: 'application/gzip'})
bodyParser.json({type: 'application/json'})
bodyParser 有没有办法在 API 端点处理这两种情况? 任何帮助表示赞赏。
其实不用担心 我能够弄清楚那将如何发生。 我可以让 bodyParser 处理 gzip 和 json POST 有效载荷主体。
app.use(bodyParser.json({type: 'application/gzip'}))
app.use(bodyParser.json({type: 'application/json'}))
如果请求标头中的
Content-Type
是application/gzip
,第一个解析器将处理
如果请求标头中的
Content-Type
是application/json
,则第二个将处理
对我来说,以下工作:
app.use(bodyParser.json({type: ['application/json', 'application/gzip']}))