2016-05-16 46 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

相关问题