2015-06-01 26 views
1

在API中,我的工作,有一個終點,應該是通過POST請求特別營造出宛如如何爲POST請求定義REST端點,期望單個項目和列表?

{ 
    "title": "foo title", 
    "description": "foo description" 
} 

一個單一的項目,但也喜歡

{ 
    "items": [ 
     { 
      "title": "foo title", 
      "description": "foo description" 
     }, 
     { 
      "title": "bar title", 
      "description": "bar description" 
     }, 
     { 
      "title": "buz title", 
      "description": "buz description" 
     } 
    ] 
} 
項目列表

它是否符合REST,端點期望(並處理有效)多種類型的輸入(單個項目和列表)?或者應該只在這種情況下將一個列表定義爲有效輸入?

如何滿足要求並正確設計此端點?

回答

1

我看不到有任何問題不這樣做。實際上,管理提供的內容取決於您。

也許可以爲你的項目列表提供元素的簡單數組:

[ 
    { 
     "title": "foo title", 
     "description": "foo description" 
    }, 
    { 
     "title": "bar title", 
     "description": "bar description" 
    }, 
    { 
     "title": "buz title", 
     "description": "buz description" 
    } 
] 

我寫了一些關於這個URL爲:https://templth.wordpress.com/2015/05/14/implementing-bulk-updates-within-restful-services/。請參閱「實施批量添加」一節。

希望它可以幫助你, 蒂埃裏

相關問題