我是新的回圈4 (NodeJS),我有一个问题。我正在开发一个API。如何指示post请求正文中未定义为模型的参数?
示例:
@post('/gameshits/{id}', {
responses: {
'200': {
description: 'Return the number of correct answers',
},
},
})
async gamesHits(
@param.path.string('id') id: string,
@requestBody() answers: Array<String>,
): Promise<number> {
....
}问题在于requestBody()它的编译,但是在环回/资源管理器中说它可以呈现。唯一的选择是创建一个模型?如何添加更多的参数来发送调用的主体?(不像@param do那样在url中)
谢谢。
发布于 2019-08-15 20:05:01
不,您不需要创建一个模型来指示参数,实际上它非常容易,但是没有太多的文档。
你可以指出类似的事情。
@post('/gameshits/{id}', {
responses: {
'200': {
description: 'Return the number of correct answers',
},
},
})
async gamesHits(
@param.path.string('id') id: string,
@requestBody({
content: {
'application/json': {
type: 'object',
schema: {
properties: {
gameName: {type: 'string'},
characters: {
type: 'array',
items: {
properties: {
name: {type: 'number'},
power: {type: 'number'},
cost: {type: 'number'},
ability: {type: 'string'}
},
},
},
},
},
},
},
}) data: any,
): Promise<number> {
....
}以获得这样的回送/资源管理器响应。
{
"gameName": "string",
"characters": [
{
"name": 0,
"power": 0,
"cost": 0,
"ability": "string"
}
]
}发布于 2021-10-07 03:01:08
您不需要任何文档,而是有关描述请求的swagger文档:https://swagger.io/docs/specification/2-0/describing-request-body/?sbsearch=request
然后,只需在@requestBody装饰器中应用OpenAPI规范,就像黑暗未知中提到的那样。
https://stackoverflow.com/questions/56920829
复制相似问题