URI for RESTful Web Services

2019-09-04 08:39发布

I'm trying to understand how URIs are supposed to look like. Looking at this reference: http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services, I am a bit confused by how POST is supposed to work.

The example given is this: http://example.com/resources/142. I'm associating "142" as a specific item in "resources" (thus, one could delete, update, or get the representation). The "142" is confusing to me in the context of creating a new member of a collection. What does the 142 mean? In terms of databases, would that be the new member's id? (I'm associating these with CRUD).

Can anyone clarify?

3条回答
疯言疯语
2楼-- · 2019-09-04 09:25

The example is probably a bad one. A better example in your case would be

http://example.com/users/142

In REST, this is interpretted the same as

http://example.com/users?id=142

as long on the server side you deal with the fact that the last part of the route is associated with an ID.

Both of these would identify the user 142. However, you would not generally use this with a POST, because POST is used for creation and you would not know the ID of the user you are creating at this point in most cases.

查看更多
成全新的幸福
3楼-- · 2019-09-04 09:32

In your example, I would think of 142 as the resource name. Your POST might make new instances of "142" with their own id.

查看更多
The star\"
4楼-- · 2019-09-04 09:35

I'm not quite sure I understand either, but I understand that if you POST to http://example.com/resources/142 you are treating 142 as if it were a collection in it's own right and would be adding another item to that collection.

If it were www.blog.com/blog/142

142 might be a users blog and a POST would possible create a new Blog Post.

a PUT to 142 would either Update the whole of 142 or create the blog if it didn't exist.

查看更多
登录 后发表回答