Rolle Rolle - 2 months ago 17
Python Question

how to get POST params with cherrypy

In my very simple cherrypy server I try to get the POST data of a request. I've looked around and came up with:

class UpdateScript:
def index(self):
cl = cherrypy.request.body.params
print(cl)
return ""
index.exposed = True


But all it prints is {}. What am I missing?

Edit: My c# code for sending the post request is:

var client = new WebClient();
byte[] response = client.UploadData(UpdateScriptUrl, "POST", System.Text.Encoding.ASCII.GetBytes("field1=value1&field2=value2"));

Answer

Specify desired fields as positional parameters:

class UpdateScript:
    def index(self, field1, field2):
        ...

Or as keyword arguments:

class UpdateScript:
    def index(self, **kwargs):
        ...

Then, you will get what you want.

I tested it with following python script (Python 2.7):

import urllib
print urllib.urlopen('http://localhost:8080', 'field1=b&field2=c').read()
Comments