我正在尝试确定为Restful API调用返回JSON响应的最佳做法。我已经阅读了多篇提供不同意见的博客和教程文章。
我见过的一种方法是建议任何Restful调用返回包含元数据和结果数据的JSON响应,如下图所示:
// response to a GET that returns an array of elements
{
"status": "success",
"count": 2,
"type": "LoadServingEntity",
"results":[ {
"lseId": 2756,
"name":"Georgia Power Co",
"code":"7140",
"websiteHome":"http://www.georgiapower.com/"
}, {
"lseId":1,
"name":"City of Augusta",
"code":"1000",
"websiteHome":null
}]
}
// an response that reports an API error
{
"status":"error",
"count":2,
"type":"Error",
"results":[{
"code":"NotNull",
"message":"An appKey must be supplied",
"objectName":"requestSignature",
"propertyName":"appKey"
}, {
"code":"NotNull",
"message":"An appId must be supplied",
"objectName":"requestSignature",
"propertyName":"appId"
}]
}
其他文章poo-poo这种方法。我即将开始一个新的API项目,我想从专家那里获得关于这个主题的最佳方法的反馈。
TIA