2016-05-16 48 views
1

REST API響應結構和佈局的最佳實踐是什麼?刮刮卡的什麼是REST API響應的正確方法?

例子:

成功響應

{ 
    "status": "success", 
    "data": # some data here 
} 

故障響應

{ 
    "status": "fail", 
    "data": { 
       "code": # some error code, 
       "message": # some error explaining message 
      } 
} 

回答

3

有許多方法來設計你的API響應。這是以你的架構,技術和其他方面爲條件的。關於這個話題採取

{ 
    "status": "error", 
    "code": 404, 
    "data": null, /* or optional error payload */ 
    "message": "Error xyz has occurred" 
} 

欲瞭解更多信息:

根據你的榜樣,我會迴應這樣

成功的請求:

{ 
    "status": "success", 
    "data": { 
      /* Application-specific data would go here. */ 
    }, 
    "message": null /* Or optional success message */ 
} 

失敗的請求看看這個鏈接

Standard JSON API response format?

Best Practices for Designing a Pragmatic RESTful API

REST API Error Codes 101

相關問題