2

I'm new to web-service programming, and I'm unsure of a few things. I know that REST web-services can be used to interact with the system like this:

  • to retrieve a user: GET /user?id=1

  • to retrieve all users: GET /user

  • to insert a user: PUT /user?{all user data}

When I need to do an update action for some tables using a JSON object can this be done with a REST API or does it need to be some other interface? If is REST, what is the right method (GET, POST, DELETE, etc) to use?

Is it correct to have two type of patterns in the same web-service?

2

Before I answer your question, a couple of points about your example:

  1. REST is based on resources, so the ID for e.g. a User resource would usually be part of the resource path (rather than a URL query parameter):

    GET /user/1

  2. Inserting is adding an entity to a resource collection, so to insert into e.g. a User collection you'd usually use something like:

    POST(with user data in request body) /user/

Now to answer your question, updates are modifications to existing resources, so are usually accomplished with something like:

PUT(with user data in request body) /user/1

The Wikipedia article on REST is a good high-level reference on the subject; in particular, see the Applied to web services section.

There are several good Stack Overflow questions on the topic, one of which is this one from 2010.

  • Just some clarification regarding PUT vs. POST: PUT should be an idempotent operation, i.e. sending the same values again should not yield different results, but when inserting via PUT you are in fact inserting a new user every time. tools.ietf.org/html/rfc2616#section-9.1.2 – Residuum Jul 21 '14 at 14:13
  • @Residuum - multiple PUT requests to the same URL should not insert a new entity every time. If the entity already exists, it will simply replace its current state with that given. The effect of N PUT requests to the same entity URL is the existence of the entity with the given state. – Mike Partridge Jul 21 '14 at 16:19
  • @MikePartridge In many cases, you won't set the entity ID from the client side, but on the server side, e.g. an auto-increment column in a database or something similar. There, you need a POST request to generate the ID. But that does not hold for all RESTful API, because other problems may arise: PUT requests for CouchDB are not idempotent, others may have similar solutions for concurrency. wiki.apache.org/couchdb/HTTP_Document_API#PUT – Residuum Jul 22 '14 at 8:18

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.