我在API中有一些终结点-/user/login/products

在Swagger UI中,我将emailpassword发布到/user/login,作为响应,我收到token字符串。

然后,我可以从响应中复制 token ,并希望将其用作请求中所有URL的Authorization header 值(如果存在),并以/products为例。

我应该在Swagger UI页面上的某个位置手动创建文本输入,然后将 token 放在那里并以某种方式注入(inject)请求中,或者是否有工具可以更好地管理它?

最佳答案

您可以在请求中添加 header 参数,并且Swagger-UI会将其显示为可编辑文本框:

swagger: "2.0"
info:
  version: 1.0.0
  title: TaxBlaster
host: taxblaster.com
basePath: /api
schemes:
- http

paths:

  /taxFilings/{id}:

    get:
      parameters:
      - name: id
        in: path
        description: ID of the requested TaxFiling
        required: true
        type: string
      - name: auth
        in: header
        description: an authorization header
        required: true
        type: string
      responses:
        200:
          description: Successful response, with a representation of the Tax Filing.
          schema:
            $ref: "#/definitions/TaxFilingObject"
        404:
          description: The requested tax filing was not found.

definitions:
  TaxFilingObject:
    type: object
    description: An individual Tax Filing record.
    properties:
      filingID:
        type: string
      year:
        type: string
      period:
        type: integer
      currency:
        type: string
      taxpayer:
        type: object

api - 如何在Swagger UI中发送带有请求的自定义 header ?-LMLPHP

您还可以使用apiKey类型添加安全性定义:
swagger: "2.0"
info:
  version: 1.0.0
  title: TaxBlaster
host: taxblaster.com
basePath: /api
schemes:
- http

securityDefinitions:
  api_key:
    type: apiKey
    name: api_key
    in: header
    description: Requests should pass an api_key header.

security:
 - api_key: []

paths:

  /taxFilings/{id}:

    get:
      parameters:
      - name: id
        in: path
        description: ID of the requested TaxFiling
        required: true
        type: string

      responses:
        200:
          description: Successful response, with a representation of the Tax Filing.
          schema:
            $ref: "#/definitions/TaxFilingObject"
        404:
          description: The requested tax filing was not found.

definitions:
  TaxFilingObject:
    type: object
    description: An individual Tax Filing record.
    properties:
      filingID:
        type: string
      year:
        type: string
      period:
        type: integer
      currency:
        type: string
      taxpayer:
        type: object
securityDefinitions对象定义安全方案。
security对象(在Swagger–OpenAPI中称为“安全要求”)将安全方案应用于给定的上下文。在我们的案例中,我们通过将安全性要求声明为顶级来将其应用于整个API。我们可以选择在各个路径项和/或方法中覆盖它。

这将是指定您的安全方案的首选方法。并替换第一个示例中的header参数。不幸的是,Swagger-UI至少在到目前为止的测试中没有提供文本框来控制此参数。

关于api - 如何在Swagger UI中发送带有请求的自定义 header ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41180615/

10-10 22:01
查看更多