用 swagger 发布一个 json body
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35411628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post a json body with swagger
提问by ncrocfer
I would like to POST a json body with Swagger, like this :
我想用 Swagger 发布一个 json 正文,如下所示:
curl -H "Content-Type: application/json" -X POST -d {"username":"foobar","password":"xxxxxxxxxxxxxxxxx", "email": "[email protected]"}' http://localhost/user/register
Currently, I have this definition :
目前,我有这个定义:
"/auth/register": {
"post": {
"tags": [
"auth"
],
"summary": "Create a new user account",
"parameters": [
{
"name": "username",
"in": "query",
"description": "The username of the user",
"required": true,
"type": "string"
},
{
"name": "password",
"in": "query",
"description": "The password of the user",
"required": true,
"type": "string",
"format": "password"
},
{
"name": "email",
"in": "query",
"description": "The email of the user",
"required": true,
"type": "string",
"format": "email"
}
],
"responses": {
"201": {
"description": "The user account has been created",
"schema": {
"$ref": "#/definitions/User"
}
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "#/definitions/Errors"
}
}
}
}
}
But the data are sent in the URL. Here the generated curl provided by Swagger :
但是数据是在 URL 中发送的。这里生成的卷曲由 Swagger 提供:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://localhost/user/register?username=foobar&password=password&email=foo%40bar.com'
I understand that the querykeywork is not good, but I didn't find the way to POST a JSON body. I tried formDatabut it didn't work.
我知道query关键工作不好,但我没有找到发布 JSON 正文的方法。我试过了,formData但没有用。
回答by William Cheng
You need to use the bodyparameter:
您需要使用body参数:
"parameters": [
{
"in": "body",
"name": "body",
"description": "Pet object that needs to be added to the store",
"required": false,
"schema": {
"$ref": "#/definitions/Pet"
}
}
],
and #/definitions/Petis defined as a model:
并被#/definitions/Pet定义为一个模型:
"Pet": {
"required": [
"name",
"photoUrls"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"category": {
"$ref": "#/definitions/Category"
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"xml": {
"name": "photoUrl",
"wrapped": true
},
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"xml": {
"name": "tag",
"wrapped": true
},
"items": {
"$ref": "#/definitions/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": [
"available",
"pending",
"sold"
]
}
},
"xml": {
"name": "Pet"
}
},
参考:https: //github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/2_0/petstore.json#L35-L43
OpenAPI/Swagger v2 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object
OpenAPI/Swagger v2 规范:https: //github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object
For OpenAPI v3 spec, bodyparameter has been deprecated. To define the HTTP payload, one needs to use the requestBodyinstead, e.g. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/petstore.json#L39-L41
对于 OpenAPI v3 规范,body参数已被弃用。要定义 HTTP 有效负载,需要使用requestBody代替,例如https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/petstore.json# L39-L41
OpenAPI v3 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
OpenAPI v3 规范:https: //github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject

