在Swagger中设置自定义cookie和user-agent值

5
我们正在使用Swagger进行API文档编写。
我在尝试时遇到了问题,基本上我们从Swagger调用的REST端点需要Cookie(例如cookie:token = xxxxx;)和User-Agent(User-Agent:custom values;)参数。
但是当我尝试设置此参数时, Cookie不作为请求的一部分发送。 浏览器值覆盖了User-Agent。 我在firefox和chrome上都尝试过。
我确实尝试在网上搜索,但没有找到合适的答案来解决我的问题,有人建议设置 useJQuery:true和withCredentials:true以设置cookies,但没有一个有效。
对此有什么建议吗?
1个回答

0

如其网站所述:

在 OpenAPI 3.0 中,cookie 认证是指通过 cookie 发送的 API 密钥。例如,通过名为 JSESSIONID 的 cookie 进行认证的定义如下:

openapi: 3.0.0
...
# 1) Define the cookie name
components:
  securitySchemes:
    cookieAuth:         # arbitrary name for the security scheme; will be used in the "security" key later
      type: apiKey
      in: cookie
      name: JSESSIONID  # cookie name

然后在端点级别:

paths:
     /users:
        get:
          security:
            - cookieAuth: []  # note the arbitrary name defined earlier
          description: Returns a list of users.
          responses: 
            '200':
              description: OK

对于 swagger:"2.0",您可以像这样定义 cookie 认证:

securityDefinitions:
  cookieAuth:
    type: apiKey
    name:
      Cookie # this is actually the Cookie header which will be sent by curl -H
    in: cookie

在终端引用它的方式与 OpenAPI 3 相同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接