Live data from Hacker News

Do you really know why you prefer REST over RPC?

apihandyman.io

91–100 of 124 posts

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

#91

REST as "representational state transfer" is a boolshit. The real reason it is popular - it allows to communicate via HTTP, just URLs everyone understands and many tools available, even web browser can be used. If you remember SOAP - the crazy approach to web services promoted before REST, you understand the difference REST made. The same way AJAX is not about XML, REST for me is not about "representational state tra…

[deleted]

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

#92

One point that didn't get addressed in this article is the use of HTTP status codes to indicate the result of a request. REST encourages proper use of status codes – 404 for a missing resource, 422 for invalid data, 201 for successful resource creation, etc. – further enhancing the predictability of the API. How does RPC handle status codes?

Eh, sorta. Send an invalid body, say an unavailable product ID, or an invalid quantity. What error will you return? HTTP codes and REST might make sense for some simple scenarios (like managing files), but it breaks down fast otherwise. Shoehorning everything into the few codes HTTP has seems pointless.

> Send an invalid body, say an unavailable product ID, or an invalid quantity. What error will you return?

In this case the client has sent an invalid request so the response should be 400 Bad Request or 422 Invalid Data, with details of the error in the response body.

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

#93

One point that didn't get addressed in this article is the use of HTTP status codes to indicate the result of a request. REST encourages proper use of status codes – 404 for a missing resource, 422 for invalid data, 201 for successful resource creation, etc. – further enhancing the predictability of the API. How does RPC handle status codes?

Eh, sorta. Send an invalid body, say an unavailable product ID, or an invalid quantity. What error will you return? HTTP codes and REST might make sense for some simple scenarios (like managing files), but it breaks down fast otherwise. Shoehorning everything into the few codes HTTP has seems pointless.

400, 422 (or even 404 if the product id was part of the resource URL). That's not shoehorning, that's using something which defines a crude yet semantically consistent granularity, that does not prevent having more detailed errors in the response body and/or additional headers. So I get a level of abstraction for free, increasing consistency in semantics and behaviour both inside my API and outside, easing debugging and allowing me to use off-the-shelf HTTP components (including curl) while leveraging common knowledge of the protocol across applications.

The pet peeve issue I have with RPC/HTTP APIs is that people keep reinventing HTTP on top of it, only badly (at which point they may as well use RPC/TCP). That's why I like REST/HTTP, because I can "reuse things" when the problem model fits well within that tool (and it very often does). If REST/HTTP becomes a constraint instead of an asset, then maybe one should not use either REST or HTTP, and I'm fine with it because that was not the tool for the job.

This is a tool, not a religion.

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

#95
post #90

Earlier quoted context omitted.

Why would you do this? It makes no sense. By (mostly) adhering to REST patterns you get so much stuff for free. Other developers can quickly get up to speed quickly. Client libraries are easier to write. Things like Ember Data work out of the box. I agree that every now and then doing a POST to /logout is easier than doing a DELETE /access_token/23, but a consistent API is far more worth it.

Because not every operation is CURD against one resource. E.g. sending money, transfer item, bulk modify A while update B, etc.

Sending money and transfer item are a create operation against one resource. The same resourse actually, called transfer. That records who transfers what to whom and how much. Thats exactly how it is done in banking.

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

#96
post #73

Earlier quoted context omitted.

> The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload. So PUT either creates or replaces a resource. Definition of replaced: > 1. take the place of. Source: https://www.google.com.au/search?q=define%3A+replaced It is pretty clear to me that the spec says PUT completely replaces the state of a resource…

so since the post I replied to said that POST is for create, and PUT is for complete replacement, and you just indicated that PUT can be used in ways that the OP did not, and I objected to the OP's prescriptivism, you agree with me despite your stance of arguing with me about it.

Yes I seem to have misinterpreted your response. Apologies.

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

#97
post #82
post #71

It seems to be that many of the comments on this and other API posts recently are by people who have never had to integrate with some else's "mostly REST but only when it suits me" API. If the API you are writing has these features: * you are the consumer as well as the author. * no other developer is ever going to need to understand it. * don't care about server/proxy/library support. by all means don't bother with…

Let's say you have to integrate with my API that uses GET /user?id=1 instead of /user/1 , How have I made your life worse? Is it going to cost you any extra time at all to code it up?

People usually miss the references that should be encoded within REST responses, The hypermedia part. You should be able to call /user/ and get a list of users whith links to the specific users. This link will be in the correct http-format which means you can discover the api from one or a few starting points. This gets even more obvious when you try to fetch a sub-resource or call a "method" on a resource. Those links are encoded within the resource itself. That means when you get a user record it may contain links to things you can do with it (add an address or an extra email, get specific details etc). With SOAP it is only naming and there is very little recommendations about that.

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

#98
post #82
post #71

It seems to be that many of the comments on this and other API posts recently are by people who have never had to integrate with some else's "mostly REST but only when it suits me" API. If the API you are writing has these features: * you are the consumer as well as the author. * no other developer is ever going to need to understand it. * don't care about server/proxy/library support. by all means don't bother with…

Let's say you have to integrate with my API that uses GET /user?id=1 instead of /user/1 , How have I made your life worse? Is it going to cost you any extra time at all to code it up?

Querystrings for resources are technically okay in REST:

> The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource . . .

source: https://tools.ietf.org/html/rfc3986#section-3.4

With a caveat, you will note the "non-hierarchical data" bit.

It means that this:

    GET /user/1/groups
is valid and this:

    GET /user?id=1&groups
is an anti-pattern.

Why? The spec doesn't say of course but at a guess I'd say because there is already a hierarchical data format in URLs, the path. This assumption is build into every web framework & language I've ever used, with query strings being passed as unordered dictionaries.

As you suggest the convention is to only use query strings as search parameters and the like. Personally I think conventions are useful to follow, especially when they don't cost you anything like the one we're talking about.

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

#99

The Richardson Maturity Model, as explained by Martin Fowler, is the only explanation of the benefits of REST that has ever made sense to me: http://martinfowler.com/articles/richardsonMaturityModel.htm... And for what it's worth, I've only found value in levels 1 and 2, not level 3. Also, this talk by DHH helped me understand how to organize an API by creating more nouns with a restricted set of verbs, instead of pr…

Why do you not find value in hypermedia? Do you have tightly coupled servers and clients?

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

#100
I'm amazed that when talking about RPC that schemas never came up. Schemas are probably my favorite part of RPCs. Let's say you have multiple services that need to communicate, but testing interoperability between the services is tricky. RPC type enforced schemas (interface definition language (IDL)) are great for this because the schemas provide guarantees that the services are communicating correctly. If a system starts seeing exceptions about IDL/schema errors, then you know right away that things are broken. Writing tests that can assume that RPC method calls will be enforced.

Another thing that struck me strange about the article is showing the RPC urls. When I was using Thrift, I never needed to think about URLs. I just needed to make sure that I was calling methods correctly.

I think RPCs are a good fit for organizations looking for a way for their service based architecture to communicate. I don't really like exposing RPCs as a public API or for web clients to interact with.

Post reply on HN