没有 content-type header 的表达解析 JSON
Express parse JSON without content-type header
我有一个 Express API 正在替换现有的 API。现有 API 接收 JSON 数据但不需要 content-type header。 Express 似乎需要此 header 才能将 body 解析为 JSON 并返回未定义。
有没有办法让 Express 假设数据是没有 content-type header 集的 JSON 类型?
请求主体的解析是使用 body-parser
library which has an option to change the allowed content-type header values 完成的。这里有一个例子,用一个总是 returns true 的函数替换检查,从而总是试图将正文解析为 json.
const bodyParser = require('body-parser');
app.use(bodyParser.json({
type(req) {
return true;
}
}))
我有一个 Express API 正在替换现有的 API。现有 API 接收 JSON 数据但不需要 content-type header。 Express 似乎需要此 header 才能将 body 解析为 JSON 并返回未定义。
有没有办法让 Express 假设数据是没有 content-type header 集的 JSON 类型?
请求主体的解析是使用 body-parser
library which has an option to change the allowed content-type header values 完成的。这里有一个例子,用一个总是 returns true 的函数替换检查,从而总是试图将正文解析为 json.
const bodyParser = require('body-parser');
app.use(bodyParser.json({
type(req) {
return true;
}
}))