API 设计 - json_api 最佳实践 return 无数据
API design - json_api best practice to return no data
我有 Rails 应用程序也有一个 API。我一直在尝试遵循 http://jsonapi.org 的整体结构,但在没有任何数据的情况下,我找不到任何关于结果的指南。例如,我有一个如下所示的端点:
https://[root]/api/apps/34/localized_strings?from_date=1330776000
其中 from_date
是 unix 时间戳,如果服务器根据此日期值更新或更新数据我 return 所有数据,它没有更新我想要 return 没有数据。我想知道这样做的最佳方法是什么。当前结果如下所示:
{
"data": []
}
在没有变化或数据的情况下,基于 http://jsonapi.org 代替 return "status": 204
的状态会更好或更常规吗?
只是returnhead :no_content
,所以rails会return204状态码空内容
这里有一个销毁的例子
def destroy
# Destroy stuff
# Return 204, no content
head :no_content
end
According to the specification,表示空集合的响应为:
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"links": {
"self": "http://example.com/articles"
},
"data": []
}
服务器必须响应成功请求以使用资源对象或null
提供的资源对象获取单个资源作为响应文档的主要数据。
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"links": {
"self": "http://example.com/articles/1/author"
},
"data": null
}
我有 Rails 应用程序也有一个 API。我一直在尝试遵循 http://jsonapi.org 的整体结构,但在没有任何数据的情况下,我找不到任何关于结果的指南。例如,我有一个如下所示的端点:
https://[root]/api/apps/34/localized_strings?from_date=1330776000
其中 from_date
是 unix 时间戳,如果服务器根据此日期值更新或更新数据我 return 所有数据,它没有更新我想要 return 没有数据。我想知道这样做的最佳方法是什么。当前结果如下所示:
{
"data": []
}
在没有变化或数据的情况下,基于 http://jsonapi.org 代替 return "status": 204
的状态会更好或更常规吗?
只是returnhead :no_content
,所以rails会return204状态码空内容
这里有一个销毁的例子
def destroy
# Destroy stuff
# Return 204, no content
head :no_content
end
According to the specification,表示空集合的响应为:
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"links": {
"self": "http://example.com/articles"
},
"data": []
}
服务器必须响应成功请求以使用资源对象或null
提供的资源对象获取单个资源作为响应文档的主要数据。
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"links": {
"self": "http://example.com/articles/1/author"
},
"data": null
}