为什么Swagger编辑显示" undefined"具有自定义标头的操作的标头?

时间:2017-04-03 13:49:14

标签: swagger-2.0 swagger-editor

我使用在线Swagger编辑器(http://editor.swagger.io)和heroku-pets.yaml示例。我更改了GET /操作以引入自定义标头token

paths:
  /:
    get:
      parameters:
        - name: limit
          in: query
          description: number of pets to return
          type: integer
          default: 11
          minimum: 11
          maximum: 10000
        - name: token
          in: header
          description: token
          type: string

但是,当我这样做时,#34;尝试出来",出现错误"找不到错误服务器或发生错误"," Headers&#34 ;说undefined

Code manipulated for testing purpose

知道可能出现的问题以及解决方法吗?

1 个答案:

答案 0 :(得分:1)

通过Ajax直接从浏览器发送的请求受CORS(跨域请求共享)规则的约束。作为其中的一部分,目标服务器可以指定请求中允许哪些HTTP头。在您的情况下,目标主机 <sd:DepartmentReference> <sd:DepartmentIdentifier>7IM0</sd:DepartmentIdentifier> <sd:DepartmentUUIDIdentifier>95837aa7-947f-4a00-9600-0000012c0001</sd:DepartmentUUIDIdentifier> <sd20080201:DepartmentLevelIdentifier>Afdelings-niveau</sd20080201:DepartmentLevelIdentifier> <sd:DepartmentReference> <sd:DepartmentIdentifier>7IMU</sd:DepartmentIdentifier> <sd:DepartmentUUIDIdentifier>9f5a74a7-947f-4a00-b600-000001320001</sd:DepartmentUUIDIdentifier> <sd20080201:DepartmentLevelIdentifier>NY3-niveau</sd20080201:DepartmentLevelIdentifier> <sd:DepartmentReference> <sd:DepartmentIdentifier>7PKU</sd:DepartmentIdentifier> <sd:DepartmentUUIDIdentifier>c28ffaa0-947f-4a00-8000-000001320001</sd:DepartmentUUIDIdentifier> <sd20080201:DepartmentLevelIdentifier>NY4-niveau</sd20080201:DepartmentLevelIdentifier> </sd:DepartmentReference> </sd:DepartmentReference> </sd:DepartmentReference> <sd:DepartmentReference> <sd:DepartmentIdentifier>10FF</sd:DepartmentIdentifier> <sd:DepartmentUUIDIdentifier>320e71a7-947f-4a00-a200-000001340001</sd:DepartmentUUIDIdentifier> <sd20080201:DepartmentLevelIdentifier>Afdelings-niveau</sd20080201:DepartmentLevelIdentifier> <sd:DepartmentReference> <sd:DepartmentIdentifier>10FE</sd:DepartmentIdentifier> <sd:DepartmentUUIDIdentifier>2f176da7-947f-4a00-8600-000001340001</sd:DepartmentUUIDIdentifier> <sd20080201:DepartmentLevelIdentifier>NY3-niveau</sd20080201:DepartmentLevelIdentifier> <sd:DepartmentReference> <sd:DepartmentIdentifier>10ØV</sd:DepartmentIdentifier> <sd:DepartmentUUIDIdentifier>6252c1a6-947f-4a00-ae00-000001360001</sd:DepartmentUUIDIdentifier> <sd20080201:DepartmentLevelIdentifier>NY4-niveau</sd20080201:DepartmentLevelIdentifier> </sd:DepartmentReference> </sd:DepartmentReference> </sd:DepartmentReference> 仅允许petstore-api.herokuapp.comContent-Type请求的标头,这可以通过执行preflight OPTIONS请求来查看:

/pet

这就是您尝试使用自定义标头时出现错误的原因。

要测试自定义标头,您需要一个可以接受自定义标头的test server。一个这样的服务器是令人敬畏的http://httpbin.org,它将回应请求中发送的数据。所以你可以使用这样的东西:

> curl -X OPTIONS http://petstore-api.herokuapp.com/pet

HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,PUT,POST,DELETE
Access-Control-Allow-Headers: Content-Type     <-------------
Allow: GET,POST,PUT,GET
Content-Type: text/html; charset=utf-8
...