Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know if it's the "best" way, but here's how I do it:</p> <pre><code>import cherrypy class RESTResource(object): """ Base class for providing a RESTful interface to a resource. To use this class, simply derive a class from it and implement the methods you want to support. The list of possible methods are: handle_GET handle_PUT handle_POST handle_DELETE """ @cherrypy.expose def default(self, *vpath, **params): method = getattr(self, "handle_" + cherrypy.request.method, None) if not method: methods = [x.replace("handle_", "") for x in dir(self) if x.startswith("handle_")] cherrypy.response.headers["Allow"] = ",".join(methods) raise cherrypy.HTTPError(405, "Method not implemented.") return method(*vpath, **params); class FooResource(RESTResource): def handle_GET(self, *vpath, **params): retval = "Path Elements:&lt;br/&gt;" + '&lt;br/&gt;'.join(vpath) query = ['%s=&gt;%s' % (k,v) for k,v in params.items()] retval += "&lt;br/&gt;Query String Elements:&lt;br/&gt;" + \ '&lt;br/&gt;'.join(query) return retval class Root(object): foo = FooResource() @cherrypy.expose def index(self): return "REST example." cherrypy.quickstart(Root()) </code></pre> <p>You simply derive from the <code>RESTResource</code> class and handle whichever RESTful verbs you desire (GET, PUT, POST, DELETE) with a method of the same name prefixed with <code>handle_</code>. If you do not handle a particular verb (such as POST) the base class will raise a <code>405 Method Not Implemented</code> error for you.</p> <p>The path items are passed in <code>vpaths</code> and any query strings are passed in in <code>params</code>. Using the above sample code, if you were to request <code>/foo/bar?woo=hoo</code>, <code>vpath[0]</code> would be <code>bar</code>, and <code>params</code> would be <code>{'woo': 'hoo'}</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload