r/javascript • u/deepsyx • Oct 31 '17
You Give REST a Bad Name
https://www.youtube.com/watch?v=nSKp2StlS6s35
u/paypaypayme Oct 31 '17
method names in URIs? Sounds like someone has a CGI masquerading as a RESTful endpoint
8
30
u/hhh333 Oct 31 '17
As someone who was expecting to talk to a REST API for integration but ended being served a non-conform SOAP webservice that also contains unicode within xml tags, this song made my day.
The kicker? In the envelope, it's quoted JSON.
6
u/kowdermesiter Oct 31 '17
Once I had to work with XML in CSV. Yeah! I asked where this is coming from and it was so high up the food chain that I said fuck it.
1
Nov 01 '17
Where I work we just had a data leak because some of our data files had fields that were position-based...I would kill for something as structured as CSV in that case LOL. Thankfully we are using better tech in other areas
0
Nov 01 '17
[deleted]
3
u/thndrchld Nov 01 '17 edited Nov 01 '17
I have walked this dark path before.
It was a bulk data importer for expressionengine, which lacked a native importer. The guy who wrote it chose to create a very xml-like CSV schema. I inherited the project before he finished it, so I had to finish it the way he designed it.
Later, I asked him why he chose the weird CSV format instead of XML and just using the native XML functions the language had built-in.
He got all wide-eyed, then sighed and buried his face in his hands and said something along the lines of "I didn't even think of that."
I don't remember exactly what the data looked like, and don't have any examples laying around, but it looked something like:
import,,,,,,,, ,BEG REC >>>,205,,,,,, ,,,BEG FLD >>>,1945,,, ,,,,,DAT TYPE,string,, ,,,,,,,content,"This is a record" ,,,<<<END FLD,1945,,, ,<<<END REC,205,,,,,, ,BEG REC >>>,206,,,,,, ,,,BEG FLD >>>,1973,,, ,,,,,DAT TYPE,int,, ,,,,,,,content,865 ,,,<<<END FLD,1973,,, ,<<<END REC,206,,,,,, ,,,,,,,,, ,,,,,,,,,Future import/exporters were switched to XML.
17
Oct 31 '17
This is the nerdiest thing I've ever seen in my life but I love it!
9
u/anticucho Oct 31 '17
Hate to be a one upper: https://www.youtube.com/watch?v=Fow7iUaKrq4
3
Oct 31 '17
Omg that hurts to watch! Haha
6
u/ric2b Oct 31 '17
If you're interested in nerdy music, there's this group called "DualCore" that raps about hacking: https://youtu.be/X0WNQ3UYbJE
I hope they show up on Mr. Robot someday.
1
9
u/0x13mode Oct 31 '17
"Hey, coders, e-mail and go home. All in all it's just an enterprise waterfall!" https://www.youtube.com/watch?v=Mq1MD5qXI08
5
u/Mentioned_Videos Oct 31 '17 edited Nov 01 '17
Other videos in this thread:
| VIDEO | COMMENT |
|---|---|
| Enterprise Waterfall | +5 - "Hey, coders, e-mail and go home. All in all it's just an enterprise waterfall!" |
| Monzy performs at Stanford Univ. | +3 - Hate to be a one upper: |
| Akira: Tetsuo's End | +2 - http://www.youtube.com/watch?v=l1URmUxNdqg |
| Dual Core - "All The Things" | +2 - If you're interested in nerdy music, there's this group called "DualCore" that raps about hacking: I hope they show up on Mr. Robot someday. |
| We're Gonna Build a Framework | +1 - http://www.youtube.com/watch?v=Wm2h0cbvsw8 |
| I'm a bot working hard to help Redditors find related videos to watch. I'll keep this updated as long as I can. |
2
u/r0ck0 Nov 01 '17
Can someone explain why method names in URLs is a bad thing?
Obviously you need to whitelist methods for security. But are there other reasons?
Is it about being more flexible?
8
u/Sector_Corrupt Nov 01 '17
It's no longer a REST API at that point, just a RPC (remote procedure call) type API. A REST-like API at a minimum should generally constrain endpoints to representing some sort of objects, and the expected payload & return values should be the state of the object.
Some of the fields being passed back might come from methods, and modifying values on these objects might set off methods behind the scenes, but in general the goal is that the frontend shouldn't need to know about any of the methods involved, they should just be able to fetch or modify the state of an object and have it handled etc.
5
u/losingthefight Nov 01 '17
No. The R in URI/L is Resource. So your URL should be a resource (/users/1, /posts/32/comments, etc). The HTTP Method (GET, PUT, POST, DELETE, PATCH, etc) is the action on that end point. Generally, the HTTP verb is what you want to do on the resource. For example, GET post /posts/32. POST a new post at /posts. If you put the method name in the URL you are a) exposing your internal logic and making your code brittle and b) breaking what most consumers would expect and c) making it so that consumers need to know the internal implementation of the API rather than what they do. There is a pig difference between POST /posts to create a post and POST /posts/createNewPost (why wouldn't it be /post/create or /post/createPost, for example).
Hope that helps!
1
u/r0ck0 Nov 01 '17
Ah so "method" refers to http verbs in the video?
I thought it meant method/function names in server-side classes/objects.
2
u/OmegaVesko Nov 01 '17
No, it does mean having URLs built around methods that exist somewhere on the server, instead of the resources they represent. The client shouldn't know or care about how an API is implemented internally, it's similar to your URLs all being paths to
.phpor.aspxfiles.1
u/r0ck0 Nov 01 '17
I was just thinking that it's pretty common in web frameworks to have controller/action naming conventions that correlate with URLs by default. i.e. In Phalcon the URL
/hello/doggywould execute the PHP class method:class HelloController { public function doggyAction() {} }Obviously you can use routes on top of that for custom URLs for more flexibility.
I guess with an API it makes more sense to always define all routes for flexible longevity?
-1
u/Isvara Nov 01 '17
There is a [big] difference between POST /posts to create a post and POST /posts/createNewPost
Actually, there isn't. REST doesn't care what your identifiers look like.
4
Nov 01 '17
Yes. It does. URLS are resources. What is a createNewPost? Why does POSTing to /posts fail? If I am POSTing a createNewPost what does that mean?
2
u/losingthefight Nov 01 '17
Technologically, no difference, since you can route it however you please. Architecturally and conceptually, there is a huge difference. /createNewPost isn't a resource, it is an action. Keep the URL/URIs as just that: resources. Use the HTTP verbs to say what you want to do on that resource.
1
u/Isvara Nov 01 '17
You're conflating two different things. The resource is abstract and conceptual. URIs are not resources, the are resource identifiers. You could make the URI path a UUID or a random string and it wouldn't make a difference. In fact, that's arguably better, because it discourages people from constructing URIs based on their pattern—a big no-no with REST.
The important thing about the REST style is not the resource identifier (which is effectively opaque), but the semantics of the resource. You should be transferring a representation of some state, regardless of whether you identify the resource as
/postsor/post/createPost. Your semantics don't change just because the URI changed.People don't understand this, because they think REST is that thing with the pretty URIs that everyone started doing when Rails came out.
4
2
2
u/markasoftware Oct 31 '17
I've never understood why REST is so widely used. What if I have an error for which there is no suitable HTTP code? Or operations for which there is no suitable HTTP method?
14
Nov 01 '17
Can you describe an error that doesn't fit an HTTP code? 400s cover user input error, 500 covers server error. You can even throw the one that makes the most sense and send back a response body with more info about the error, you dont have to just look at the status code to see what happened.
2
u/markasoftware Nov 01 '17
Well, I guess you can just say
400or500if you don't have a more specific one...but for example, just today I've been coding up an API, and what code should I give when there are missing JSON parameters that are required? I just give a 400 because there's nothing better to do.6
u/delventhalz Nov 01 '17 edited Nov 01 '17
HTTP status codes aren’t really part of the REST specification, btw.
But generally speaking, the best practice is to use less HTTP codes, not more. The idea is that they are broad and prescribe general solutions. 400: the client is bad and needs to modify their request. 503: the server is busted, but if you try the exact same request later, it might work. You should avoid the more esoteric codes because the solution may not be well understood by your client. 400, 401, 403, 404, 500, and 503 will probably have you covered.
Now if you want to get really specific, that’s great too, but you should come up with custom codes and error messages to send back. No general standard will ever have everything you need anyway. Twitter’s ApI follows this strategy.
As for operations for which there are no HTTP verbs (also not part of the REST spec), there aren’t any. At least none I’ve ever run into. Like the status codes, they are a limited set of broadly applicable categories, and have very explicit behaviors clients can rely on (PUT is idempotent, PATCH is not, etc etc). The idea is not to describe exactly what the route does, it never could, but to prescribe a widely understood set of behaviors.
4
u/grinde Nov 01 '17
The codes themselves are supposed to be general, and 400 ("Bad Request") is the correct code for a... bad request. Additional information is supposed to be in the message body.
2
Nov 01 '17
Well 400 means Bad Request, as in the user submitted incorrect data, so that is what you want. As for what else you return to explain to the user what happened thats up to you. Personally my errors look something like this:
404 response:
{ "name": "APIError", "code": "NotFound", "status": 404, "message": "Resource Not Found", "success": false }Which provides a program readable error code, user readable message, and a boolean to help you parse whether this is a successful call or error (which you can tell by the http status but here you dont have to care what the http status is at all)
2
u/losingthefight Nov 01 '17
I don't understand why 400 wouldn't be the right code in that case. If they are missing a required field, it should be 400 (input error) with a message coming back saying "xyz is required" or something. That's what I do for missing required inputs.
4
u/ShadowsSheddingSkin Nov 01 '17 edited Nov 01 '17
I've never understood why REST is so widely used.
Same reason most of the world uses the metric system. Is it objectively the best possible system? Probably not. Is it a product of universal truths or some fundamental mathematical laws? Nope. It's convenient and works pretty well for most things we need it to do, though, and the biggest benefit of having a standard is just having a standard. It means that, so long as everyone actually understands how it works - which, obviously, not everyone does - everyone is (metaphorically) speaking the same language. This is more useful than most people ever appreciate.
To overuse the metric system example, one of the most expensive programming mistakes of the last century was the result of one program returning imperial units when another assumed the numbers it was receiving were metric. Both pieces of code worked perfectly, interacted correctly, but because neither of them considered the possibility that the other might be doing things differently - because someone at Lockheed skimmed the contract or just didn't think, I guess - the US government flew two hundred million dollars into Mars.
As for the rest:
What if I have an error for which there is no suitable HTTP code?
The existing codes are supposed to be broad enough to apply to everything. For things where you feel the need to be especially specific and really think it's important, feel free to make your own, just be prepared for the top comment the next time this is reposted being about how someone had to work with an API with a different HTTP code for every damn error.
Or operations for which there is no suitable HTTP method?
The four existing verbs are also supposed to be broad enough that they can apply to everything. It should be possible to model any behavior as a change in state.
1
u/dvlsg Nov 01 '17
I usually throw a JSON body in with http error responses so clients can parse additional data / subtypes / error messages / etc. It works okay.
1
u/Isvara Nov 01 '17
I've never understood why REST is so widely used.
It isn't. Read the chapter and think about whether you've actually seen anyone doing that.
What is widely used is something called REST, but would be more accurately described as "using HTTP correctly".
1
u/Fern_Silverthorn Nov 01 '17
Custom HTTP methods then. That's what Internet of things devices do in that case. I can't imagine that very many people need them though. The default methods are quite generic.
1
u/Arancaytar Oct 31 '17
Currently writing a client for querying MS Dynamics CRM; this hits close to home.
1
-16
u/cyberst0rm Oct 31 '17
the obsession with REST is tiring.
Its no more than a standard convention that can make certain tedious tasks easier. It helps people spend less time learning and more time doing.
But like any convention, if its not well understood, if people claim to follow it but make tons of exceptions, then it doesn't look useful.
If everyone had REST compatible projects and followed REST api standards, there wouldn't be any problems.
People are what's wrong, not some arbitrary convention.
19
9
12
0
Oct 31 '17
[deleted]
9
u/OmegaVesko Oct 31 '17
Look at the guy's other uploads, I think it's a pretty safe assumption to make that he sang this himself.
3
80
u/mothzilla Oct 31 '17
I worked at a company that had an API endpoint for every individual property of an object. (Think /object/5/getColour, object/5/getSize, object/5/getPrice)
They also used POST as a get mechanism. Sometimes. They thought their API was the bees testicles.