大摇大摆地发布json正文

大摇大摆地发布json正文,json,rest,api,swagger,openapi,Json,Rest,Api,Swagger,Openapi,我想发布一个带有招摇的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": { "t

我想发布一个带有招摇的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 -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://localhost/user/register?username=foobar&password=password&email=foo%40bar.com'

我知道
query
keywork不好,但我没有找到发布JSON正文的方法。我尝试了
formData
,但没有成功。

您需要使用
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"
  }
},
参考:

OpenAPI/Swagger v2规范:

对于OpenAPI v3规范,
body
参数已被弃用。要定义HTTP负载,需要使用
requestBody
,例如


OpenAPI v3规范:

如何使用jquery ajax调用或获取发布“requestBody”??我正在尝试,但swagger api接收参数始终为空请尝试openapi生成器生成typescript jquery、typescript获取客户端,看看这是否是您想要的。