Why does the Facebook API use POST for updating records?

ぃ、小莉子 提交于 2021-02-10 23:43:10

问题


For example:

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

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?

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?

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.


回答1:


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.

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}

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.



来源:https://stackoverflow.com/questions/25391621/why-does-the-facebook-api-use-post-for-updating-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!