【发布时间】: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