如何在Swagger UI中发送带有请求的自定义标头? [英] How to send custom headers with requests in Swagger UI?

查看:89
本文介绍了如何在Swagger UI中发送带有请求的自定义标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some endpoints in the API - /user/login, /products.

在Swagger用户界面中,我将emailpassword发布到/user/login,作为响应,我收到了token字符串.

In Swagger UI I post email and password to /user/login and as a response I receive a token string.

然后,我可以从响应中复制令牌,并希望将其用作请求中所有URL的请求中的Authorization标头值,并以/products为例.

Then, I can copy the token from the response and want to use it as Authorization header value in requests to all urls if it's present, and to /products as an example.

我应该在Swagger UI页面上的某个位置手动创建文本输入,然后将令牌放到那里,然后以某种方式注入请求中,还是有工具以更好的方式对其进行管理?

Should I create a text input manually somewhere on the Swagger UI page, then put the token there and somehow inject in the requests or are there tools to manage it in a better way?

推荐答案

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

You can add a header parameter to your request, and Swagger-UI will show it as an editable text box:

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

您还可以添加类型为apiKey的安全性定义:

You can also add a security definition with type 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.我们可以选择在各个路径项和/或方法中覆盖它.

The security object (called "security requirements" in Swagger–OpenAPI), applies a security scheme to a given context. In our case, we're applying it to the entire API by declaring the security requirement a top level. We can optionally override it within individual path items and/or methods.

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

This would be the preferred way to specify your security scheme; and it replaces the header parameter from the first example. Unfortunately, Swagger-UI doesn't offer a text box to control this parameter, at least in my testing so far.

这篇关于如何在Swagger UI中发送带有请求的自定义标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆