3

I'm looking at implementing a new RESTFul call where a User can follow/unfollow a generic 'thing' item, but I need to know the best or common approach below, 1 or 2?

1) A GET or POST on the following URL's:

user/{thing_to_follow_id)/follow
user/{thing_to_follow_id)/unfollow

Or

2) A POST containing thing_to_follow_id

POST
user/follow

A DELETE on

user/follow/{thing_to_follow_id}

The first seems the most restful, but it feels wrong to post an empty payload where the second allow me to do a GET and return all things a user is following.

What is the best approach here?

| improve this question | | | | |
4

Per the HTTP spec, GET calls should be safe - that is,

the client does not request, and does not expect, any state change on the origin server as a result of applying a safe method to a target resource

Furthermore, one of the principles of REST is that URIs point to things (nouns), rather than to actions (verbs). In your case, the user would have a collection of things that they follow. That might look more like:

add a new thing to the collection of things the user follows
POST /user/follows 
{
    thing
}

get all the things the user follows
GET /user/follows

stop following a thing
DELETE /user/follows/{id}

Another option would be to use a mapping resource, such as:

add a new thing to the collection of things the user follows
POST /user-follows
{
    user
    thing
 }

 get all the things the user follows
 GET /user-follows?userId={id}

 get all the users following a thing
 GET /user-follows?thingId={id}

 stop following a thing
 DELETE /user-follows/{id}
 DELETE /user-follows?thingId={id}&userId={id}
| improve this answer | | | | |
  • @svarog raises a good point. In some of these cases - those where you have all the information on the client - PUT would be superior to POST because of its idempotency guarantee. – Eric Stein Jan 9 '17 at 14:42
  • Yes, that makes sense! GET should always be safe and the fact a user is 'creating' a new object should not be done with a GET. But, what worries me is that I would also need to find out the 'follow ID' to unfollow an item or edit it. For example, if a user is following an 'Article' to unfollow it I would need to know they were first following it. I guess that's an easy problem to resolve tho. – MrKnotts Jan 9 '17 at 15:29
1

You can think of the mapping between a user and a thing to follow as a unique resource in of itself, which can have two different states, either true or false depending on whether the user is following the thing in question.

So a way of doing this would be for the client to PUT a new state of that resource on the server, since the client should already know the location of the resource. Your URL schema could be something like

/users/<user_id>/things/<thing_id>

or something similar that conveys that this is a resource representing the state of mapping between user and thing.

A GET on this resource would tell you if User A is following Thing B

Request
  GET /users/<user_id>/things/<thing_id>

Response
  200 OK
  {following: true}

And the client can change that state by PUTing a new state

Request
  PUT  /users/<user_id>/things/<thing_id>

  {following: false}

Response
  204 No Content
| improve this answer | | | | |
0

As you are basically updating/creating some line in some table when a user decides to follow or unfollow an item, you shouldn't be using GET.

Rather user POST or PUT

POST or PUT:
user/{thing_to_follow_id)/follow

PUT:
user/{thing_to_follow_id)/unfollow

And unless you are actually deleting something (if someone else can later choose to follow that item) then don't use a DELETE.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.