2017-04-03 44 views
0

我正在使用在線Swagger編輯器(http://editor.swagger.io)和heroku-pets.yaml示例。我改變了GET /操作引入自定義標題token爲什麼Swagger Editor會爲自定義標題的操作顯示「未定義」標題?

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 

但是當我做「試試吧」,有一個錯誤「錯誤找不到服務器或發生錯誤」,並且「頭」說undefined

Code manipulated for testing purpose

任何想法可能是錯誤的,如何解決呢?

回答

1

通過Ajax從瀏覽器直接發送的請求受限於CORS(跨源請求共享)規則。作爲其中的一部分,目標服務器可以指定請求中允許使用哪些HTTP頭。在你的情況下,目標主機petstore-api.herokuapp.com只允許Content-Type頭的請求/pet,可以通過做一個preflight OPTIONS請求中可以看出:

> 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 
... 

這就是爲什麼你試圖用一個自定義標題時出現錯誤。

要測試自定義標題,您需要一個可以接受自定義標題的test server。一個這樣的服務器是令人敬畏的http://httpbin.org,它會迴應請求中發送的數據。所以你可以使用這樣的東西:

swagger: '2.0' 
info: 
    title: test 
    version: '1.0' 
host: httpbin.org 
schemes: [http, https] 
produces: 
    - application/json 
paths: 
    /get: 
    get: 
     summary: Returns GET data 
     parameters: 
     - name: token 
      in: header 
      type: string 
     responses: 
     200: 
      description: OK 
+0

謝謝海倫!你能否給我提供一個例子,頭部和查詢參數是按請求的形式傳遞的,並獲取對象數組作爲響應。知道我高壓實施上述的情況下,我在預檢期間得到404錯誤。 –