【问题标题】:How to send body data from swagger using Node.js/Express?如何使用 Node.js/Express 从 swagger 发送正文数据?
【发布时间】:2017-11-28 04:36:49
【问题描述】:

这是我的招摇配置:

/**
 * @swagger
 * /api/addData:
 *    post:
 *     consumes:
 *       - text/html
 *     produces:
 *       - text/html
 *     parameters:
 *       - name: author
 *         in: body
 *         required: true
 *         schema:
 *           type: string
 *       - name: email
 *         in: body
 *         required: true
 *         schema:
 *           type: string
 *       - name: text
 *         in: body
 *         required: true
 *         schema:
 *           type: string
 *     tags:
 *       - Create single message
 *     description: Creates single message
 *     responses:
 *       200:
 *    description: You have created new message
 */

我还有下一个后端代码:

app.post('/api/addData', function (req, res) {
    console.log(req.body); // {}
})

一切似乎都很好,但是当我尝试使用 Swagger UI 执行请求时,我的控制台中出现了一个空对象。 问题是,如何配置 swagger 以便他可以从我的身体发送数据?

【问题讨论】:

    标签: javascript node.js express swagger


    【解决方案1】:

    我已经用下面的代码解决了这个问题:

    /**
    * @swagger
    *  /api/addData:
    *    post:
    *      summary: Creates a new message.
    *      consumes:
    *        - application/json
    *      tags:
    *        - Create a new message
    *      parameters:
    *        - in: body
    *          name: user
    *          description: The user to create.
    *          schema:
    *            type: object
    *            required:
    *              - name
    *              - email
    *              - text
    *            properties:
    *              name:
    *                type: string
    *              email:
    *                type: string
    *              text:
    *                type: string
    *      responses:
    *        201:
    *          description: New message created!
    */
    

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 2019-08-27
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多