开发者

Rails RESTful API + support for clients that only allow GET/POST - Is it possible?

I'm publishing an API for my Rails app, and I'm trying to be as good and RESTful as possible, while being realistic at the same time. I know that Flash and some other clients have problems doing anything other than GET and POST, but I'd still like to offer the more RESTful interface to clients that can be that expressive.

The optimal solution, in my m开发者_如何学Cind, would be to set up the normal Rails RESTful routing, but also allow an optional query-string parameter, like method, that would allow for clients to use POST but mimic another verb.

Is there an (easy) way to do this with Rails routing? Anyone else doing something similar?


Yes, the client can specify the header X-Http-Method-Override. If the header's value is put then rails will route the request as a PUT.

There is also a parameter, _method, that should theoretically work, but in some cases it doesn't (if you add it to the query string of a POST request it does not work). There is some rack middleware that fixes those cases: https://github.com/baroquebobcat/rack-methodoverride-with-params

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜