Hapi-Swagger 因标头值而失败

ppb*_*ppb 1 node.js swagger-ui hapijs hapi-swagger

我在我们的应用程序中使用 hapi-swagger,其中一个 API 尝试使用自定义标头,但是当我使用自定义标头调用该 API 时出现以下错误

{
"statusCode": 400,
"error": "Bad Request",
"message": "Invalid request headers input"
}
Run Code Online (Sandbox Code Playgroud)

在我使用带有验证器的标头的 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()
                }
            }                               
        }
    }
Run Code Online (Sandbox Code Playgroud)

以下是我们正在使用的版本。

"hapi": "17.2.2",

"hapi-swagger": "9.1.1",

"joi": "13.1.2",

Rya*_*yan 5

我最近遇到了这个。您需要使用allowUnknown验证选项来允许未知标头(https://github.com/hapijs/hapi/issues/2407#issuecomment-74218465)。

validate: {
    headers: Joi.object({
        name: Joi.string().required()
    }).options({ allowUnknown: true })
}
Run Code Online (Sandbox Code Playgroud)

另请注意,hapi 17 更改了报告验证错误的默认行为。如果您想记录或返回指示哪些标头验证失败而不是通用“错误请求”的实际错误,您可以添加自定义处理程序failActionhttps://github.com/hapijs/hapi/issues/3706)。