简体   繁体   中英

REST : different representations of the same data

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

  1. Can I keep the same url for both requests, or should keep them seperate?
  2. 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.
  3. How to detect the difference in data type at client (html/Json)\\

thanks,

bsr.

Similar question: REST Content-Type: Should it be based on extension or Accept header?

The accepted answers has great points.

Can I keep the same url for both requests, or should keep them seperate?

Yes, keep them the same. Its the same resource, you're just asking for different representations of it.

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.

You can use the Accept header to specify the return content-type.

How to detect the difference in data type at client (html/Json)\\

You would look at the "Content-Type" header.

如何为输出类型添加变量?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM