tags:

views:

289

answers:

2

I have the basics of a REST service done, with "standard" list and GET/POST/PUT/DELETE verbs implemented around my nouns.

However, the client base I'm working with also wants to have more powerful operations. I'm using Mongo DB on the back-end, and it'd be easy to expose an "update" operation. This page describes how Mongo can do updates.

It'd be easy to write a page that takes a couple of JSON/XML/whatever arguments for the "criteria" and the "objNew" parts of the Mongo update function. Maybe I make a page like http://myserver.com/collection/update that takes a POST (or PUT?) request, with a request body that contains that data. Scrub the input for malicious querying and to enforce security, and we're done. Piece of cake.

My question is: what's the "best" way to expose this in a RESTful manner? Obviously, the approach I described above isn't kosher because "update" isn't a noun. This sort of thing seems much more suitable for a SOAP/RPC method, but the rest of the service is already using REST over HTTP, and I don't want users to have to make two different types of calls.

Thoughts?

+1  A: 

Typically, I would handle this as:

  • url/collection
  • url/collection/item

  • GET collection: Returns a representation of the collection resource

  • GET collection/item: Returns a representation of the item resource (optional URI params for content-types: json, xml, txt, etc)

  • POST collection/: Creates a new item (if via XML, I use XSD to validate)

  • PUT collection/item: Update an existing item

  • DELETE collection/item: Delete an existing item

Does that help?

Mr-sk
wouldn't that be: "POST collection: Creates a new item, redirects to collection/item"
Javier
Yeah sorry, lemme update. heh. Good catch.
Mr-sk
I have that much already set up. However, if you check the mongo doc I linked to, that update function provides much more functionality than the above-described REST setup. I basically want to expose the update function over HTTP, and I'm not sure about the clearest/"best" way to do it.
Tim Ridgely
+1  A: 

Since as you're aware it isn't a good fit for REST, you're just going to have to do your best and invent a standard to make it work. Mongo's update functionality is so far removed from REST, I'd actually allow PUTs on the collection. Ignore the parameters in my examples, I haven't thought too hard about them.

  • PUT collection?set={field:value}
  • PUT collection?pop={field:1}

Or:

  • PUT collection/pop?field=1
Michael