为什么 Facebook API 使用 POST 来更新记录? [英] Why does the Facebook API use POST for updating records?

查看:85
本文介绍了为什么 Facebook API 使用 POST 来更新记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

https://developers.facebook.com/docs/graph-api/reference/v2.1/page/locations

来自 Rails/REST 背景,我的印象是使用 PUT 或 PATCH 进行更新是最佳实践.所以我想知道,为什么 Facebook 只使用 POST 进行更新?

Coming from a Rails/REST background, I was under the impression using PUT or PATCH for updates was best practice. So I'm wondering, why did Facebook just use POST for updates?

是不是因为使用 POST 和 GET 比 4 或 5 个不同的 HTTP 方法更简单?还是因为他们支持一些只有这些的设备?或者他们打算改用 PUT/PATCH?有什么想法吗?

Is it because it's just simpler to have POST and GET, rather than 4 or 5 different HTTP methods? Or is it because they're supporting some devices that only have those? Or are they planning to move to using PUT/PATCH? Any ideas?

只使用 POST 来创建和更新记录似乎很好,所以您不必关心实现,但想知道这背后的原因是什么.

It seems nice to use just POST for both creating and updating records, so you don't have to care about the implementation, but wondering what the reason is behind this.

推荐答案

他们不使用 PUT 或 PATCH,因为 URL 不指向特定项目的实际位置,而是整个集合.项目的实际 ID(主键,如果需要)是消息正文的一部分.

They don't use PUT or PATCH, because the URL does not point to the actual location of the specific item, but the whole collection. The actual ID (primary key, if you want) of the item is part of the message body.

在 FB 示例中,似乎需要 page-id、main_page_id、store_number 和 location_page_id 来识别资源.page-id 已经存在了,但是为了使用 PUT,他们还必须将其他的也放在 URL 中:

In the FB example it seems that page-id, main_page_id, store_number and location_page_id are all required to identify the resource. page-idis already there, but in order to use PUT, they would have had to put the others in the URL as well:

PUT /v2.1/{page-id}/locations/{main_page_id}/{store_number}/{location_page}

出于某种原因,他们不想走那条路.所以它变得丑陋,因为他们将项目的有效载荷与其地址混合在一起.一般而言,人们期望消息正文的内容包含对 URL 指向的地址处的资源所做的更改.项目的 ID 地址,因此是不可变的,因此它不属于正文.

For some reason, they didn't want to go that way. So it got ugly, since they mix the payload of the item with it's address. Generally speaking, one expects the content of the message body to contain the changes made to the resource at the address the URL is pointing to. An item's ID is the address and thus immutable, so it doesn't belong in the body.

这篇关于为什么 Facebook API 使用 POST 来更新记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆