简体   繁体   中英

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 mind, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM