RESTful parameters - Why input parameters are so "webish"?


#1

Hi,

Using Rails 1.2.3, I would like to know if it is possible to set the
format of parameters for a POST call.

Like you have:

GET /assets/#

def show
@asset = Asset.find(params[:asset])
respond_to |format|
format.html
format.xml {render :xml => @asset.xml}
end
end

why the format of params[:asset] in

POST /assets

def assets
@asset = Asset.new(params[:asset])

end

needs to have a very “webish” format:
“asset[title]=foo&asset[description]=bar” ? Why not xml/json? Is it
possible to have a prefilter to accept xml, json …? Or better,
formatting the input parameters according to Accept header value? If it
is not supported, how would you intercept the parameters to convert them
from xml/json to this array format ?

Regards,
Jean-Etienne


#2

On 4/21/07, Jean-Etienne D. removed_email_address@domain.invalid
wrote:

respond_to |format|

end

needs to have a very “webish” format:
“asset[title]=foo&asset[description]=bar” ? Why not xml/json? Is it
possible to have a prefilter to accept xml, json …? Or better,
formatting the input parameters according to Accept header value? If it
is not supported, how would you intercept the parameters to convert them
from xml/json to this array format ?

Sure can! You can post XML (the same format as the @asset.to_xml
output) if you set the content-type to application/xml, or append a
.xml extension. ActiveResource works like this. There’ s a param
parser for Mime::XML that parses the xml and returns a hash in the
same format, so your controller code doesn’t need to change. All you
need is a respond_to block for the response.

I have an experimental plugin for JSON support:
http://svn.techno-weenie.net/projects/plugins/json_for_rails, but
there are a few issues:

  • It uses the fjson gem (fast json parser written in c). I seem to
    recall rails has this now, buy converting json to YAML and returning
    that.
  • json has no date/time literal. If you look at my plugin, it looks
    for any strings in the xmlschema format and parses them to date/times.
    But, this is a hack. Currently there’s no standard for times in
    json, so I don’t really think json is suitable for this task. You can
    come up with your own convention if you like, or just use XML and get
    on with your app.
  • set up a simple json param parser. This would be a good guide if
    you wanted a simple way to process other common formats, for example.


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com


#3

Hi,

This is what I tought at the beginning, I tried to pass the xml I got
from a GET (application/xml, not assets.xml) but with no luck. Checking
the logs, the xml is understood as a simple string…

I remind you I am not working on Rails…

Regards,