假设我有4个表:课程,教程,测验和教程 - 测验。
关系如下:
现在,对于特定课程,我可以通过一个操作为多个测验(批量)分配多个教程。在为教程分配测验时,会在教程 - 测验表中添加新记录。
执行此批量分配的适当方法和资源名称是什么?
PATCH /courses/id/tutorials/quizzes
PATCH /courses/id/quizzes/tutorials
PATCH /courses/id/tutorials-quizzes
这question与我的要求有关,但地雷略有不同,因为我在操作中为两个不同的集合分配了多个项目。
答案 0 :(得分:2)
您可以使用POST或Patch
发表:强> POST方法通常用于在列表资源上使用时添加元素,但您也可以支持此方法的多个操作。
POST /batch
[
{ method: 'POST', path: '/items', body: { title: 'foo' } },
{ method: 'DELETE', path: '/items/bar' }
]
POST/courses/id/tutorials/quizzes
<强>修补程序:强> 在这种情况下,没有必要定义您的格式来描述更新。 使用PATCH方法也是合适的,因为相应的请求对应于部分更新。根据RFC5789(http://tools.ietf.org/html/rfc5789)
PATCH /items
[ { id: 1, name: 'foo' }, { id: 2, name: 'bar' } ]
PATCH /courses/id/tutorials/quizzes
示例:Google Drive API
POST https://www.googleapis.com/batch
Accept-Encoding: gzip
User-Agent: Google-HTTP-Java-Client/1.20.0 (gzip)
Content-Type: multipart/mixed; boundary=END_OF_PART
Content-Length: 963
--END_OF_PART
Content-Length: 337
Content-Type: application/http
content-id: 1
content-transfer-encoding: binary
POST https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id
Authorization: Bearer authorization_token
Content-Length: 70
Content-Type: application/json; charset=UTF-8
{
"emailAddress":"example@appsrocks.com",
"role":"writer",
"type":"user"
}
--END_OF_PART
Content-Length: 353
Content-Type: application/http
content-id: 2
content-transfer-encoding: binary
POST https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false
Authorization: Bearer authorization_token
Content-Length: 58
Content-Type: application/json; charset=UTF-8
{
"domain":"appsrocks.com",
"role":"reader",
"type":"domain"
}
--END_OF_PART--
https://developers.google.com/drive/v3/web/batch
https://saw.saas.hp.com/help/en/full/Content/8000_DeveloperGuide/ApiRESTBulkUpdate.htm
资源命名指南:
http://www.restapitutorial.com/lessons/restfulresourcenaming.html