问题描述
表结构
session
-------------------------
id | name | date
-------------------------
speaker
-------------------------
id | name
-------------------------
session_speaker
-------------------------
session_id | speaker_id
-------------------------
请求方法到位
a) GET /speakers (all speakers)
b) GET /speakers/:id (details on specific speaker)
c) GET /speakers/:id/sessions (list of sessions of a speaker)
问题
我应该发出哪种类型的请求以表明我不仅需要发言者的详细信息,而且还需要会议的内容,本质上是将呼叫(b)和(c)的结果合并为一个.
在实际的商业项目中该如何做?客户端进行两次调用(b& c),或者他们开发了另一个REST端点,因此将b& c的结果合并在一起. c一次通话?
还是客户应该提出两个请求?
实际上,您可以指定要在响应有效负载中包含哪些字段作为查询参数.像这样:
GET /speakers/someid?fields=firstname,lastname,age,sessions
另一种方法(更好的方法)应该是利用标头Prefer
(请参阅此链接以获取规范: https://tools.ietf.org/html/rfc7240 )来指定要在响应有效负载中包含的详细信息种类:
-
return=minimal
:仅演讲者提示 -
include=sessions
:演讲者提示和他/她的会话
以下是示例:
Get /speakers/someid HTTP/1.1
Host: api.example.org
Content-Type: application/json
Prefer: include=sessions
Vary: Prefer,Accept,Accept-Encoding
Irakli Nadareishvili在这个主题上写了一篇很棒的博客文章: http://www.freshblurbs.com/blog/2015/06/25/api-representations-prefer.html .
希望它对您有帮助,蒂埃里
Table structure
session
-------------------------
id | name | date
-------------------------
speaker
-------------------------
id | name
-------------------------
session_speaker
-------------------------
session_id | speaker_id
-------------------------
Request methods in place
a) GET /speakers (all speakers)
b) GET /speakers/:id (details on specific speaker)
c) GET /speakers/:id/sessions (list of sessions of a speaker)
Question
What type of request should I make to indicate I need not only the detail of the speaker but also it's sessions, essentially combining results of call (b) and (c) into one.
Also how is it done in real commercial projects? The client makes two calls (b & c) or do they develop another REST endpoint hence combing the results of b & c in a single call?
Or should the client make two requests?
In fact, you could specify as query parameters which fields you want to have within your response payload. Something like:
GET /speakers/someid?fields=firstname,lastname,age,sessions
Another approach (a better one) should be to leverage the header Prefer
(see this link for the specification: https://tools.ietf.org/html/rfc7240) to specify which kind of details you want to have within your response payload:
return=minimal
: only the speaker hintsinclude=sessions
: speaker hints and his / her sessions
Here is a sample:
Get /speakers/someid HTTP/1.1
Host: api.example.org
Content-Type: application/json
Prefer: include=sessions
Vary: Prefer,Accept,Accept-Encoding
Irakli Nadareishvili wrote an awesome blog post on this subject: http://www.freshblurbs.com/blog/2015/06/25/api-representations-prefer.html.
Hope it helps you,Thierry
这篇关于多对多关系的REST请求类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!