12

I've never built a RESTful API before and I am wondering how discrete should it be?

For example lets say I have a customer which has a name, address, phone number, email address, language, etc.

Does it make sense that there is a way to update each individual field (update address, update email address, etc), or should there just be a single update for the entire customer and each field is optional?

  • 3
    What part of REST do you want? If you just need a simple API not worrying about the standards you can handle it just like the accepted answer of thorsten müller. If you want a real REST you need to search for Roy Fielding (+dissertation for example). He is the inventor of REST. You will see that for example the idea that a resource == a table is not the truth. You will also find that for example for your question a PATCH HTTP method is quite common to use for updating one or more fields. Where the POST or PUT (lots of debate here) method can be used for complete updates on a resource. – Luc Franken Jan 5 '14 at 10:56
17

You have one update action for a resource. As a rough rule of thumb a table in your database will be a resource. So in your example there is one controller for the Customer with one update action and fields are optional as far as possible.

There can be exceptions to this (for example if you update the password you expect password & password_confirmation), but this holds true for most cases.

It is more likely that the exceptions are controllers that have limited actions for some resources, like a SessionsController that can only be created and destroyed. It is very unlikely that you would want to have actions for single fields.

In your example one thing could be handled as a resource of its own and that is the address. If you would have a table for addresses, especially if there would be more than one address per customer (billing, delivery...) this could be handled as a nested resource. But again one update action for the whole address resource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.