Routing error for RESTful API - Rails
I am creating an API for a web application that I have been working on for a while. I started with the User model. The user-side API will allow remote clients to a) retrieve user data, b) update user information, and c) create new users. I got it all to work, but it doesn't seem to be setting it up correctly. Here are my questions:
- Should the API endpoint be user or user? What's the best practice?
- I need to add an action to the end and I expect the request type to be selected instead, so I don't have to explicitly specify it. How do I properly configure my routes to not include a protected action method?
Let me give you a few examples:
Get a request to show - you want it to work without "show"
- curl -u rmbruno: blah http: //app.local/api/users/show
Put an update request - you want it to work without "updating"
- curl -X put -F 'user [forum_notifications] = true' -u rmbruno: blah http: //app.local/api/users/update
Create - Works with or without creating what I want for all these actions.
- curl -X post -F 'user [login] = mamafatta' -F 'user [email] =zzz@zzz.com ' -F 'user [password] = 12345678' http: //twye.local/api/users/
How to structure routes so that you don't require an action name? Isn't this the usual way to use RESTful APIs?
Here is my route for the API now:
map.namespace :api do |route|
route.resources :users
route.resources :weight
end
I am using calm authentication which handles HTTP authentication in curl. Any guidance on routes and best practice regarding singular and plural would be really helpful.
Thanks! -A
a source to share
-
Singular and multiple endpoints for residual APIs depend on the model you are returning. Your URL should state that there can be multiple instances of the user resource.
-
Your routes are configured correctly. GET and PUT requests must include the user ID at the endpoint, i.e.
curl -u rmbruno: blah http: //app.local/api/users/1
See this answer for an overview of why rails create these "friendly" URLs for CRUD actions: Rails - Redundant RESTful Actions for map.resources? (new, create)
a source to share