在Swagger中更改为Url-Form-Encoded Post请求

时间:2016-05-25 11:45:25

标签: post swagger

我想知道是否可以在Swagger中创建一个url-form编码的帖子请求?例如:

POST https://url.co.uk
Host: server.example.com
Authorization: Bearer <Initial Access Token>
Content-Type: application/x-www-form-urlencoded

&grant_type=password
&client_id=<client id>
&client_secret=<client secret>

目前我们有各种参数的布局,但不确定如何更改为url-form-encoded。我们已尝试更改为in:body而不是标头,但这不起作用。请参阅grant-type parameter

的示例
          "parameters": [
                {
                    "in": "header",
                    "name": "grant_type",
                    "description": "Indicates that the app is using the Resource Owner Password \"password\".",
                    "required": true,
                    "type": "string",
                    "enum": [
                        "password"
                        ] 
                },

目前返回此内容:

Accept: application/json
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,fa;q=0.6,sv;q=0.4
Cache-Control: no-cache
Connection: keep-alive
Origin: http://editor.swagger.io
Referer: http://editor.swagger.io/
User-Agent: Mozilla/5.0 
grant_type: password
client_id: xxx-xxx-xxx
client_secret: <client_secret>

对此有任何帮助/见解将不胜感激 - 甚至让我知道它是否可能?

2 个答案:

答案 0 :(得分:5)

可能,您只需要将参数的in值设置为formData,如{{1}中的OpenAPI(fka.Swagger)规范中所述} object description

  

当application / x-www-form-urlencoded或multipart / form-data用作请求的内容类型时,用于描述HTTP请求的有效负载(在OpenAPI的定义中,使用属性一个行动)

您还可以阅读part 5 section 1.7 of my OpenAPI specification tutorial

以下是一个例子:

Parameter

答案 1 :(得分:1)

对于OpenAPI 3.0.X-以下示例应有帮助

openapi: 3.0.3
info:
  title: Sample Service
  description: Lorem ipsum dolor sit amet, recusabo gloriatur ius ne. 
  version: 1.0.0
tags:
  - name: tag1
    description: tag1 desc
paths:
  /test:
    post:
      tags:
        - tag1
      summary: generates token
      description: ''
      operationId: token
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                attr1:
                  type: string
                attr2:
                  type: string
                language:
                  type: string
                  default: en
                  enum:
                    - en
                    - es
              required:
                - language
      responses:
        '200':
          description: Sample response
        '400':
          description: Invalid response

externalDocs:
  url: 'https://github.com/'
  description: 'Additional info'

When rendered the swagger looks like