CherryPy POST Request with Parameters, 400 response

Lately I've been setting up a simple HTTP web service using CherryPy (I've simplified the code):

from controllers import MyController
import cherrypy

if __name__ == '__main__':
    server_config = {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 60000
    }

    cherrypy.tree.mount(MyController(), '/Home', {})
    cherrypy.config.update(server_config)
    cherrypy.engine.start()

where MyController has an Index method:

class MyController:
    @cherrypy.expose
    def Index(self):
        pass

and I was calling the service from Python using the requests library:

requests.get('http://localhost:60000/Home/Index', data={'name': 'foo'})

but when I changed the request to use POST:

requests.post('http://localhost:60000/Home/Index', data={'name': 'foo'})

my Index method was no longer being called; the service was returning a 400 error. Thanks to this answer I tried adding **kwargs to my service method:

class MyController:
    @cherrypy.expose
    def Index(self, **kwargs):
        pass

and the Index method was now being called! That fixed the problem.

Comments

Leave a comment

What color are green eyes? (spam prevention)
Submit
Code under MIT License unless otherwise indicated.
© 2020, Downranked, LLC.