2015-06-18 28 views
9

我想通過不同的參數傳遞不同的響應,但是某些操作不起作用。Apiary.io - 具有不同參數的多個響應(200)

這是我的API:

## Question [/questions/{question_id}] 

A Question object has the following attributes: 

+ Parameters 
    + question_id: `1` (number, required) - ID of the Question in form of an integer 

### View a Questions Detail [GET] 

+ Request 

+ Header 

    X-Custom-Header : 1 

+ Response 200 (application/json) 

     { 
      "id": "1", 
      "name": "Marco" 
     } 


+ Request 

+ Header 

    X-Custom-Header : 2 

+ Response 200 (application/json) 

     { 
      "id: "2", 
      "name": "Lucas" 
     } 

但調用/問題/ 1 /問題/ 2的響應總是相同的時:

{ 
    "id": "1", 
    "name": "Marco" 
} 

有什麼不對?

謝謝

回答

3

你的藍圖沒有錯。我很擔心Apiary Mock很簡單,並且總是返回指定的第一個響應(內容協商允許)作爲默認值。

請參閱「調用非默認響應」位於Apiary http://support.apiary.io/knowledgebase/articles/117119-handling-multiple-actions-on-a-single-resource以瞭解如何調用(按需)另一個響應。

還要注意有一個提議語法API藍圖明確說明什麼的參數值依賴於特定的反應 - https://github.com/apiaryio/api-blueprint/issues/58

但是是否會蜂房的這個模擬起飛的好處是目前還不清楚。

+0

謝謝Zdenek的回覆。我仍然無法理解如何實現我的API,即使閱讀「調用非默認響應」。你能提供一個例子嗎?我相信有相當多的人在尋找這個例子。 – MeV

+0

調用非默認響應的唯一方法是使用不同的代碼和響應類型,因此我認爲不可能用代碼200返回兩個響應並鍵入application/json,是嗎? – MeV

+0

重要的是瞭解Apiary不提供API的實現。它只給你一個模擬服務器用於原型設計。這個實現真的取決於你(儘管你可以並且應該用Dred等工具來測試它)。 – Zdenek

2

我相信這是一個簡單的解決方案要做到這一點,而不使用標題:

創建不同的資源(每個記錄),所以每個人都會生成一個URL。

## Note20 [/notes/20] 

### Get notes20 [GET] 

+ Response 200 (application/json) 

     { 
      "id" : 20, 
      "name" : "note xxxx" 
     } 

## Note21 [/notes/21] 

### Get notes21 [GET] 

+ Response 200 (application/json) 

     { 
      "id" : 21, 
      "name" : "note yyyyy" 
     } 
+2

是的,這將工作,如果你不介意在文檔中擁有多個相似的資源 – Zdenek