主体解析器忽略类型参数
body-parser ignores type parameter
我有 Content-Type: application/vnd.surveymonkey.response.v1+json
的入站请求。我正在尝试对它们使用 body-parser
。 body-parser
的 json
工厂方法的 documentation 声明:
Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option.
[...]
The type option is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, type option is passed directly to the type-is library and this can be an extension name (like json), a mime type (like application/json), or a mime type with a wildcard (like / or */json). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. Defaults to application/json.
我尝试了各种参数,但无论我输入什么,解析器中间件都会继续接受 application/json
而拒绝 application/vnd.surveymonkey.response.v1+json
。事实上,当我在 type
函数中放置一个 console.debug
语句时,它从不打印,所以看起来 type
参数被完全忽略了。
我做错了什么吗?我需要做什么才能让 body-parser
遵守 type
参数?
import * as bodyParser from 'body-parser';
import * as express from 'express';
const app = express();
app.use(
bodyParser.json({
// type: 'application/vnd.surveymonkey.response.v1+json', // doesn't work
// type: ['application/vnd.surveymonkey.response.v1+json'], // doesn't work
// type: ['application/*'], // doesn't work
// type: (req) => /application\/(.+\+)?json/.test(req.headers['Content-Type'] as string), // doesn't work
type: (req) => console.debug('Hello world!'),
})
);
app.post(`/`, (req, res) => {
console.log('Request: ', req.body);
res.status(200).send();
});
It turns out 问题是我在 Firebase Functions 上 运行,它已经在入站请求上运行 body-parser
。添加 body-parser
的另一个实例是空操作,这就是它被忽略的原因。
我有 Content-Type: application/vnd.surveymonkey.response.v1+json
的入站请求。我正在尝试对它们使用 body-parser
。 body-parser
的 json
工厂方法的 documentation 声明:
Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option.
[...]
The type option is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, type option is passed directly to the type-is library and this can be an extension name (like json), a mime type (like application/json), or a mime type with a wildcard (like / or */json). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. Defaults to application/json.
我尝试了各种参数,但无论我输入什么,解析器中间件都会继续接受 application/json
而拒绝 application/vnd.surveymonkey.response.v1+json
。事实上,当我在 type
函数中放置一个 console.debug
语句时,它从不打印,所以看起来 type
参数被完全忽略了。
我做错了什么吗?我需要做什么才能让 body-parser
遵守 type
参数?
import * as bodyParser from 'body-parser';
import * as express from 'express';
const app = express();
app.use(
bodyParser.json({
// type: 'application/vnd.surveymonkey.response.v1+json', // doesn't work
// type: ['application/vnd.surveymonkey.response.v1+json'], // doesn't work
// type: ['application/*'], // doesn't work
// type: (req) => /application\/(.+\+)?json/.test(req.headers['Content-Type'] as string), // doesn't work
type: (req) => console.debug('Hello world!'),
})
);
app.post(`/`, (req, res) => {
console.log('Request: ', req.body);
res.status(200).send();
});
It turns out 问题是我在 Firebase Functions 上 运行,它已经在入站请求上运行 body-parser
。添加 body-parser
的另一个实例是空操作,这就是它被忽略的原因。