[英]Hapi-Swagger failing with header value
我在我們的應用程序中使用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”,
我最近遇到這個問題。 您需要使用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 )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.