我用 swagger 做了一个项目,只是为了测试 swagger,当我尝试发布问题时,我得到了 400 bad??
我从数组获取数据没有任何问题。
{
"message": "Validation errors",
"errors": [
{
"code": "INVALID_REQUEST_PARAMETER",
"errors": [
{
"code": "REQUIRED",
"message": "Value is required but was not provided",
"path": [
"paths",
"/recipe",
"post",
"parameters",
"0"
]
}
],
"in": "body",
"message": "Invalid parameter (body): Value is required but was not provided",
"name": "body",
"path": [
"paths",
"/recipe",
"post",
"parameters",
"0"
]
}
]
}
recipe.js 文件:
module.exports.createRecipe = (req, res) => {
let newRecipe = req.body;
newRecipe.id = recipeList.length;
newRecipe.isPublic = true;
recipeList.push(newRecipe);
return res.status(204).end();
};
paths:
/recipe:
x-swagger-router-controller: repice
get:
description: Return all the recipes
operationId: getAllRecipes
responses:
200:
description: Success get all the recipes
schema:
type: array
items:
$ref: "#/definitions/Recipe"
500:
description: Unexpected Error
schema:
type: object
properties:
messeage:
type: string
post:
description: Create one new Recipe
operationId: postRecipes
parameters:
- in: body
name: body
description: The recipe to be added
required: true
schema:
$ref: "#/definitions/Recipe"
responses:
204:
description: Success adding the rescipe
500:
description: Unexpected Error
schema:
type: object
properties:
messeage:
type: string
我需要做什么才能让它发挥作用???
我需要做什么才能做到
我预计会收到 204 请求,并且配方将被添加到数组中
您的 swagger POST 错误 400 是请求正文中缺少必填字段的典型案例。消息“值是必需的,但未提供”是您的线索。检查
/recipe
POST 操作的 swagger 定义,并确保所有必填字段都包含在请求负载中。当测试需要特定数据结构的端点时,经常会发生这种情况。根据 swagger 文档仔细检查您的请求。