如何构建一个 API,其中相同的数据可以以不同的格式(RESTful 格式)请求。例如。
GET /person/<id> //get the details of resource <id>
现在,根据客户端(浏览器)的要求,数据可以作为 html(例如正常渲染)或 Json(例如 ajax 调用)发送。所以我的疑问是
- 我可以为两个请求保留相同的网址,还是应该将它们分开?
- 如何在服务器端检测请求是否为html/Json。请求类型相同(GET)。那么我应该考虑哪个参数。
- 如何检测客户端数据类型的差异(html/Json)\
谢谢,
bsr。
How to structure an API where the same data may request in different format, in a RESTful format. For example.
GET /person/<id> //get the details of resource <id>
Now depending on the client (browser) requirement, the data may send as html (say normal rendering) or Json (say ajax call). So my doubts are
- Can I keep the same url for both requests, or should keep them seperate?
- How to detect whether the request is for html/Json at the server. The request type is same (GET). So which parameter should I consider.
- How to detect the difference in data type at client (html/Json)\
thanks,
bsr.
发布评论
评论(2)
类似的问题: REST 内容类型:它应该基于扩展还是 Accept 标头?
接受的答案有很多要点。
是的,保持它们相同。它是相同的资源,您只是要求它的不同表示形式。
您可以使用 Accept 标头指定返回内容类型。
您将查看“Content-Type”标头。
Similar question: REST Content-Type: Should it be based on extension or Accept header?
The accepted answers has great points.
Yes, keep them the same. Its the same resource, you're just asking for different representations of it.
You can use the Accept header to specify the return content-type.
You would look at the "Content-Type" header.
添加输出类型变量怎么样?
What about adding a variable for output type?