These are chat archives for cherrypy/cherrypy

12th
Dec 2017
Brig Young
@Sonophoto
Dec 12 2017 06:11
Hi, I've been studying the documentation and guessing and testing but I am at a loss of how I get to the data sent in a POST or how to define that my end point is a POST endpoint not GET. I do have GET working. I read the REST example but that is not what I am trying to do. I just want three end-point named members in a single class. I'll be back tommorrow, Thanks! :-)
Jason R. Coombs
@jaraco
Dec 12 2017 13:30
@Sonophoto There are a few ways to go about it and CherryPy doesn’t necessarily lead you to one solution. If you’re writing a REST API, it may be worthwhile considering the MethodDispatcher, but it sounds like you’ve considered that and that approach is in conflict with your existing design. I’ve encountered that as well. The cheap and dirty thing to do is simply switch on if cherrypy.request.method == ‘POST’ in your handler (Members.index or similar).
I recently took another approach… which I’d like to present for critique and consideration.
What I really need is a blog post...
But I haven’t the time for that. Here’s the gist - https://gist.github.com/d60ed19473696919a6664faf7032525b
Jason R. Coombs
@jaraco
Dec 12 2017 13:36
That approach isn’t clean - by wrapping the methods, it (a) implies that the handler is exposed and (b) might interfere with other decorators and tools, and (c) binds the method explicitly. I haven’t tested it on but Python 3.6.
Let me know if you find it useful… and I may publish it as a separate library or as part of CherryPy.
Actually, now that I think about it more, the main issue is that it’s not honoring the CherryPy dispatch semantics, so it may not work well at all when the different methods accept different parameters.
CherryPy will discover ‘index’ and think that’s the handler, but when it calls it, a different method gets called.
Jason R. Coombs
@jaraco
Dec 12 2017 13:41
But it’s during dispatch that the parameters to the handler are matched with the request. So I suspect this technique will only work if every handler has the same signature.
Brig Young
@Sonophoto
Dec 12 2017 19:26
Jason, Thanks for your time to write this up! I'll work through this now. I was reading the code last night looking for enlightenment but hadn't gotten my head wrapped around it yet. I've used Cherrypy several times at work to publish various things on the internal network just using the top level API and some static pages / Cheetah templates. All of that has worked great all along, but currently I need to deal with a POST and was at a loss how to do that in a simple fashion similar to another framework named after a horn shaped container ;-)
I will work through your gist as soon as I get the current project running. Thanks again!
Brig Young
@Sonophoto
Dec 12 2017 23:05
So this gist shows that cherrypy receives the variables however they are sent. So if I specify a *args or a **kwargs either way it is just sitting there ready to use... Here is a stripped/minimal example of what I have put together for work, the index page generates an interface to the two exposed functions which are defined in the html as 'get' or 'post' : https://gist.github.com/Sonophoto/39818f1cee1ab4b6fedf010ecc7b55ae
So unlike the horn shaped container framework there is no need to specify the method, just name the vars in the exposed handler, grab the values passed from the html interface (which specified the method in the forms) and use them. #Golden. Its good to feel dumb :sparkles: