2

I am implementing an HTTP REST API which contains a /feed request. The request returns a user's news feed.

The request comes with a few parameters, including the userId, a list of followers, startTime and maxItems.

The easiest way to implement it on the server side (Python and Flask) would be adding a JSON payload, from which I can read the arguments.

Alas, adding a payload to a GET request is not a good idea, and it is no supported by many client libraries.

My options:

  • Making /feed a POST request. It is ugly, because POST shouldn't be used to request information from a server.
  • Splitting the /feed call to /updateFollowers (POST) and /feed (GET). It would waste time because the GET call can be made only after the POST call.

Both options seem wrong. Is there any standard way to make a GET-like call with a bunch of complex arguments?

Community
  • 1
  • 1
Adam Matan
  • 107,447
  • 124
  • 346
  • 512
  • REST doesn't really have rules as you are probably aware but I'd like to share my opinion with you: It's also quite ugly to put 'request types' in the resource like this `/getFeed`, I would just stick with `/feed`. Apart from that instead of passing the user as a parameter to `/feed` it might be better to construct your url like `/users//feed` but that kind of depends on the implementation of the rest of your service – Tim May 14 '14 at 12:01
  • @TimCastelijns the `getFeed` was a mistake - it's really `/feed`. Apart from that, `userId` is OK for a URL parameter - but how do I pass arrays in the URL? – Adam Matan May 14 '14 at 12:08
  • 1
    In that case using POST might not be such a bad idea. See http://stackoverflow.com/questions/5020704/how-to-design-restful-search-filtering – Tim May 14 '14 at 12:12
  • @TimCastelijns Seems about right. Would you like to post it as a full answer so I can upvote/accept it? – Adam Matan May 14 '14 at 13:50
  • Since you must use a bunch of complex arguments, a POST request makes sense. I don't think there is any rule that says POST must never be used for read operations. However, I would ask you if there is a way you can minimize the arguments in your example. What if you just send the "userid" in GET request and let the server determine their followers etc ? For max entries, you could possibly add it to the GET request with something like /feed// where startTime and maxItems could be optional and flask view will default them to something – codegeek May 14 '14 at 13:57

1 Answers1

1

Splitting the /feed call to /updateFollowers (POST) and /feed (GET). It would waste time because the GET call can be made only after the POST call.

REST doesn't really have rules as you are probably aware but I'd like to share my opinion with you: It's quite ugly to put 'request type verbs' in the resource like this, so I wouldn't do this.

Making /feed a POST request. It is ugly, because POST shouldn't be used to request information from a server.

I understand why you think this is ugly. It sounds wrong to get data using a POST request, however, as stated in this question's top answer:

The best way to implement a RESTful search is to consider the search itself to be a resource. Then you can use the POST verb because you are creating a search. You do not have to literally create something in a database in order to use a POST.

Since this is similiar to what you're doing, using POST might not be such a bad idea. I don't see a better alternative so you probably want to implement it like this.

Community
  • 1
  • 1
Tim
  • 38,263
  • 17
  • 115
  • 131
  • `POST`-ing a search does not make a lot of sense to me, but it's probably the lesser of (quite a few) evils. – Adam Matan May 15 '14 at 12:38
  • @AdamMatan *it's probably the lesser of (quite a few) evils* I think so, yeah. You can make it make a little more sense by thinking like this *consider the search itself to be a resource* though – Tim May 15 '14 at 12:51