views:

712

answers:

2

If I understand correctly, in rest style, every query (that is, every action on every resource that does not modifies the resource's state) should be enconded in the querystring, using a get method, with no body at all...

am I right?

well, I have several applications that comunicate with the db thru an xml message that is handled by a visual basic 6 component.

the message for a query is something like this

<xml>
  <service>account</service>
  <resource>invoice</resource>
  <action>query</action>
  <parameters>
    <page>1</page>
    <page_len>10</page_len>
    <order>date</order>
    <fields>*</fields>
    <conditions>
      <date>2009-01-01..2009-01-31</date>
      <customer_id>24</customer_id>
    </conditions>
  </parameters>
</xml>

right now we are in the process of redesigning our xml messages, and we'd like to do that in such a way that they could be easily mapped to a restFul interface.

in the previous example, we need the "conditions" tags in order to prevent colissions between the parameters and the conditions (ie, what happens if I have a field named "order", "page" or something like that...

we though about sending the paramaters with a prefix, something like

http://account/invoice/?_page=1&amp;_page_len=10&amp;_order=date&amp;_fields=*&amp;date=2009-01-01..2009-01-31&amp;customer_id=24

and the xml would be something like

[...]
    <_order>date</_order>
    <_fields>*</_fields>
    <date>2009-01-01..2009-01-31</date>
    <customer_id>24</customer_id>
[...]

we are trying to define some really simple xml format for crud operations, and that the resulting xml could be easily maped to rest or json.

how would you map that kind of query in a rest application? is there some standard defined? or some page with crud rest / xml /json samples? how about returning error, or nested datasets?

thanks a lot...

A: 

You would need to url-encode the xml to be able to pass it, but, if you converted the xml to json then you could pass that string and then json->xml or json->object to process it. This would enable you to pass more complex objects around.

It isn't perfect, but works. :)

James Black
+1  A: 

IMHO in order to make your system truly RESTful you must rethink all the messages/queries that you will send.

This part:

<conditions>
  <date>2009-01-01..2009-01-31</date>
  <customer_id>24</customer_id>
</conditions>

is the tricky part. What kind of other conditions do you have? Are there many? This particular example makes me think that you can treat invoices as a subresource of a customer. When I do rest I always try to identify resource in the path and if a query stil needs any parameters, I move them to query string. So I'd write something like this:

GET /customers/24/invoices?start_date=2009-01-01&end_date=2009-01-31

Think about relations between your resources. Let's say we have resource type Foo related resource type Bar by a -to-many relation. In this case you can ask about this relation like this: GET /foo/123/bar and add query string parameters to filter it. The problem begins when you want to filter it in a way that involves relations to other resources. IMHO this means that your resource design isn't truly RESTful.

Jasiu