我在我们的应用程序中使用hapi-swagger,其中一个API试图使用自定义头,但当我调用带有自定义头的API时出现以下错误
{
"statusCode": 400,
"error": "Bad Request",
"message": "Invalid request headers input"
}下面是我在验证器中使用头的API。
{
method: 'POST',
path: '/v1/testapi',
config: {
description: 'Greet user',
notes: ['Use to greet a user'],
tags: ['api'],
handler: function ( request, h ) {
console.log('sending response...');
return h.response('OK');
},
validate: {
headers: {
name: Joi.string().required()
}
}
}
}下面是我们正在使用的版本。
"hapi":"17.2.2",
"hapi-swagger":"9.1.1",
"joi":"13.1.2",
发布于 2018-04-23 10:03:38
我最近遇到了这个问题。您需要使用allowUnknown验证选项来允许未知头(https://github.com/hapijs/hapi/issues/2407#issuecomment-74218465)。
validate: {
headers: Joi.object({
name: Joi.string().required()
}).options({ allowUnknown: true })
}还要注意,hapi 17更改了报告验证错误的默认行为。如果您想要记录或返回实际的错误,指出哪些头没有通过验证,而不是一个通用的“错误请求”,您可以添加一个自定义的failAction处理程序(https://github.com/hapijs/hapi/issues/3706)。
https://stackoverflow.com/questions/49270642
复制相似问题