Boy, if I knew then what I know now…
This is going to be a quick list of all of the tips and tricks I picked up making an API for ShareTribe Go v10. I used Grape API, which is great, though I found myself confused by the docs and wanting more. This is what I wish I knew then.
This isn’t going to be indexed nicely by Google, which prefers the title to reflect the query, so bookmark or share this if it’ll help.
Best Intro Tutorial
I tried a lot of tutorials. They mostly failed.
Grape is real old, so I suspect some of these are using prior versions and dates aren’t always forthcoming. I don’t know why they failed, I don’t really care.
What worked best was this tutorial by the illustrious Ryan Waldorf. It’s a great start and that project structure is what worked for me.
Defaults.rb is the place to add global helpers, by the by, not Base.rb.
Postman alternatives
Postman costs money. I don’t think it used to, but it’s been a while.
Since I’m using Visual Code for this (forced into it because of WSL), I downloaded the extension “Thunder client”. It’s great, I reccomend it.
No GET, only POST
I want authentication on all of my API calls, except for initially logging in. I want to send big, fat JSONs and I want them sent securely in the request body.
Somehow, for some reason, it’s utter madness to try to get the request body to load in as parameters for GET requests. For POST, PUT, etc, it’s automatic. Thus, just about every route is going to be POST.
404 Errors: no route matches…
If you have a syntax error in a Grape route, the route doesn’t register but oftentimes the app will compile. The result is a sent request getting an inexplicable 404. The 404 could be because of improper routing, but often it’s an unlogged error in your specific controller file. I guessed this when I saw some 500 errors if I hit it with a request while it was compiling, 500 errors that then turned to unhelpful 404s seconds later.
Use the tip below to get better debugging info with rake.
See Routes
“Grape on Rails Routes” is 4 years from its last update, but it works great. I saw some SO posts about getting API routes, but I thought “eh, I only have one route right now and it doesn’t even work, why should I care?” The reason you care is that running rake grape:routes
will reveal occult errors in your controller files. The working routes are also formatted oh-so-nicely.