Live data from Hacker News

Do you really know why you prefer REST over RPC?

apihandyman.io

11–20 of 124 posts

Re: Do you really know why you prefer REST over RPC?

#11
post #4
post #2

I don't think GETs should ever change state. I'm fine with all other operations being POSTs though. If nothing else it provides compatibility with clients that don't support all the HTTP verbs (yes there are some)

How do you manage big queries with GET?

With a POST. The parent just pointed out that GET should not be used for updates. They didn't say that only GET should be used for retrievals.

Re: Do you really know why you prefer REST over RPC?

#12
> If a user want to stop using your service, you’ll do this (not so obvious) call:

> DELETE /users/1234

No! It would be

  DELETE /sessions/12345
or possibly

  DELETE /users/1234/session
or even

  DELETE /users/1234/sessions/3
in the case a user can have more than one concurrent different sessions (this is actually a fairly common case for the application we do were I work. We don't use http for this though).

Unless you actually want to permanently stop the user from using the system, in which case

  DELETE /users/1234
would be the perfectly obvious choice.

Re: Do you really know why you prefer REST over RPC?

#13
A missing factor is fragility in the face of change.

RPC leads to fragile protocols. Adding arguments to a procedure or adding properties to a result can often break clients and in practice you update systems lock step.

HTTP can gracefully handle different API version requests to the same resource. REST clients are encouraged to take only what they need from the representation.

Also, I strongly disagree with the Totaling points section. Seems too "nice" to both sides.

Re: Do you really know why you prefer REST over RPC?

#14
I like how when building REST API servers and clients there are standard components that just works. The entire API in the same format means it is easy to write a DSL across the entire API configure each endpoint - such as row level and field level permissions, pagination, filtering. If you have a well designed RPC you have the same benefit, but except with REST - you can reuse someone else's work because the design is the same. It literally takes 20 seconds to change the /user/ endpoint so:

1. Non-authenticated users can create users, and see a list of users with details that are public.

2. Authenticated users can view their own public and private details and have read/write access to most fields except is_staff.

3. Staff users have full permissions for users that belong to the country they are managing.

4. Superusers have access to everything.

Without the benefit of DSLs this would take hours to write all the if/else statements and unit test them. It would be a nightmare to do the same for all your endpoints. I assume you can write some abstraction functions to use for all your RPC endpoints but it'd be easier to reuse a library from past projects that had the same REST API design.

Example:

Row level:

    permission_classes = [
        Or(And(Or(IsOwner('id'), IsStaff), IsUpdateOnly),
            And(AllowAny, IsReadOnly),
            And(AllowAny, IsCreateOnly))]
Field level:

    is_public = And(IsActive, Is('is_public', True))
    fields = {
        'display_name': [],
        ('is_active', 'password', 'email'): [
            Or(is_staff_or_owner, IsCreateOnly)
        ],
        ('full_name', 'slug',): [
            Or(is_staff_or_owner, is_public, IsCreateOnly)
        ]
    }

Re: Do you really know why you prefer REST over RPC?

#15
post #4
post #2

I don't think GETs should ever change state. I'm fine with all other operations being POSTs though. If nothing else it provides compatibility with clients that don't support all the HTTP verbs (yes there are some)

How do you manage big queries with GET?

By query, are you referring to the querystring/url size?

Practically speaking, I don't know if I've ever had a querystring that was effectively too large, busting some browser-determined limit. If you did, you might as well use a POST instead of GET, because the request is unlikely to be cacheable (I'm assuming the reason its such a large string is that you're serializing a large form of values or using the querystring to persist state in some way [protip: this is a bad idea]), which is the main reason to prefer GET for the request. POST (according to HTTP semantics, may have side effects, but is not required to have side effects).

IMHO, the choice between HTTP verbs beyond GET vs everything else is mostly bikeshedding. As a convention its fine, but there aren't a lot of pragmatic technical reasons to go with one over the other (referring to PUT vs PATCH vs POST), despite the HTTP semantics.

Re: Do you really know why you prefer REST over RPC?

#16
I really like REST most of the time, but I do have some complaints about Rails' implementation of it. I wrote about them a few years ago:

http://illuminatedcomputing.com/posts/2011/07/restless-doubt...

Basically, after form submit errors your location bar still says /widgets or /widgets/1 rather than /widgets/new or /widgets/1/edit, so bookmarking that page, or "like"ing it, or Ctrl-L + ing are all broken. I'd much prefer a redirect back to the correct URL.

Also the distinction between "create" and "update" can break the back button, if the user creates something, then clicks back to edit their submission (because submitting a second time will create a second thing, not update their original thing). That's not Rails so much as REST in general.

Re: Do you really know why you prefer REST over RPC?

#17
post #4

Earlier quoted context omitted.

How do you manage big queries with GET?

By query, are you referring to the querystring/url size? Practically speaking, I don't know if I've ever had a querystring that was effectively too large, busting some browser-determined limit. If you did, you might as well use a POST instead of GET, because the request is unlikely to be cacheable (I'm assuming the reason its such a large string is that you're serializing a large form of values or using the querystri…

No, it just was a very flexible data-retrieval API and the query strings just could get rather big.

Re: Do you really know why you prefer REST over RPC?

#18
I might be behind on state of the art these days...but my current api has no GET requests, everything is a POST. So in that term, we are not restful.

We had a couple of problems with GET requests: all of our data ended up in the url (we have too much info that has to be passed, we stopped working on some browsers), and the data returned could be cached (which is a HUGE problem for us).

Now, POSTs can also be cached, but there are easy ways around it...you can do the same thing with a GET, but that means adding more information to the url.

Anyway, one we got to the point of forgoing all GET requests, we ended up just doing everything with a POST for simplicity.

Re: Do you really know why you prefer REST over RPC?

#19

I might be behind on state of the art these days...but my current api has no GET requests, everything is a POST. So in that term, we are not restful. We had a couple of problems with GET requests: all of our data ended up in the url (we have too much info that has to be passed, we stopped working on some browsers), and the data returned could be cached (which is a HUGE problem for us). Now, POSTs can also be cached,…

I did the exact same thing in a project of mine. We're shifting away from that and put our data in cookies, so we can cache some of the things our API send out.

Re: Do you really know why you prefer REST over RPC?

#20
I generally use a pattern like:

GET /api/resource

POST /api/resource/action

Where 'action' is something a bit more descriptive than 'PUT' or 'DELETE' or whatever. Kind of like an object-oriented api... I'm still always dealing with resources, but I have custom actions for specific use cases.

Post reply on HN