【问题标题】:Swagger won't generate right json to post from yamlSwagger 不会生成正确的 json 以从 yaml 发布
【发布时间】:2018-02-20 12:30:37
【问题描述】:

我正在使用 swagger 并想使用 post api。虽然它正确显示参数,但它不会产生正确的 curl 命令将数据发送到服务器。

P.S:后端在 node.js 中,通过 swagger-jsdoc 表达和解析 YAML 文档。

这里是 YAML:

/**
 * @swagger
 * /register:
 *   post:
 *     tags:
 *       - report
 *     description: Returns info for panel api
 *     consumes:
 *       - application/x-www-form-urlencoded
 *     produces:
 *       - application/json
 *     parameters:
 *     -  name: email
 *        in: body
 *        description: Email
 *        required: true
 *        type: string
 *     -  name: password
 *        in: body
 *        description: password
 *        required: true
 *        type: string
 *     -  name: fullName
 *        in: body
 *        description: full name
 *        required: true
 *        type: string
 *     responses:
 *       200:
 *         description: An info for panel api
 *       401:
 *         description: If user didn't authenticate
 */

这是生成的curl命令:

curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'max mcgrey' 'http://localhost:5200/register'

以及回应:

{
  "status": "fail",
  "message": "Validation failed, check your inputs.",
  "errors": [
    {
      "param": "fullName",
      "msg": "Full name is required."
    },
    {
      "param": "fullName",
      "msg": "Full name must be between 1 and 50 characters long."
    },
    {
      "param": "email",
      "msg": "Email is required."
    },
    {
      "param": "email",
      "msg": "Email is required."
    },
    {
      "param": "password",
      "msg": "Password is required."
    }
  ]
}

【问题讨论】:

  • 数据应该是JSON格式??
  • 是的。但是当我将消费类型修改为“消费:-应用程序/json”时,我得到一个“curl -X POST --header 'Content-Type: application/json' --header 'Accept: text/html' -d 'max max ' 'localhost:5200/register'" 导致正文响应中出现“SyntaxError: Unexpected token”

标签: json node.js curl yaml swagger


【解决方案1】:

参数语法错误。如果操作要消耗application/x-www-form-urlencoded,则需要使用in: form参数而不是in: body

 *     consumes:
 *       - application/x-www-form-urlencoded
 *     ...
 *     parameters:
 *     -  name: email
 *        in: form              <-----------
 *        description: Email
 *        required: true
 *        type: string
 *     -  name: password
 *        in: form              <-----------
 *        description: password
 *        required: true
 *        type: string
 *     -  name: fullName
 *        in: form              <-----------
 *        description: full name
 *        required: true
 *        type: string

如果要消费 JSON,则需要一个 in: body 参数,并且各个字段(emailpassword 等)应该是 body 对象的属性:

 *     consumes:
 *       - application/json
 *     ...
 *     parameters:
 *     -  name: body
 *        in: body
 *        required: true
 *        schema:
 *          type: object
 *          required: [email, password, fullName]
 *          properties:
 *            email:
 *              type: string
 *              format: email
 *              description: Email
 *            password:
 *              type: string
 *              format: password
 *              description: password
 *            fullName:
 *              type: string
 *              description: full name

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2021-01-07
    相关资源
    最近更新 更多