【问题标题】:Post a json body with swagger大摇大摆地发布一个 json 正文
【发布时间】:2016-02-15 14:13:17
【问题描述】:

我想用 Swagger 发布一个 json 正文,如下所示:

curl -H "Content-Type: application/json" -X POST -d {"username":"foobar","password":"xxxxxxxxxxxxxxxxx", "email": "foo@bar.com"}' http://localhost/user/register

目前,我有这个定义:

"/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"
                    }
                }
            }
        }
    } 

但是数据是在 URL 中发送的。这里是 Swagger 提供的生成 curl :

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://localhost/user/register?username=foobar&password=password&email=foo%40bar.com'

我知道query 关键字不好,但我没有找到 POST JSON 正文的方法。我试过formData,但没用。

【问题讨论】:

    标签: json rest api swagger openapi


    【解决方案1】:

    需要使用body参数:

        "parameters": [
          {
            "in": "body",
            "name": "body",
            "description": "Pet object that needs to be added to the store",
            "required": false,
            "schema": {
              "$ref": "#/definitions/Pet"
            }
          }
        ],
    

    #/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 规范:https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object

    对于 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 规范:https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject

    【讨论】:

    • 如何使用 jquery ajax 调用或获取发布“requestBody”?我正在尝试,但招摇 api 接收参数始终为空
    • 请尝试 openapi-generator 生成 typescript-jquery,typescript-fetch 客户端,看看是否是你想要的。
    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多