在我非常简单的cherrypy服务器中,我尝试获取请求的POST数据.我环顾四周想出来:
class UpdateScript:
def index(self):
cl = cherrypy.request.body.params
print(cl)
return ""
index.exposed = True
Run Code Online (Sandbox Code Playgroud)
但它打印的所有内容都是{}.我错过了什么?
编辑:我发送帖子请求的c#代码是:
var client = new WebClient();
byte[] response = client.UploadData(UpdateScriptUrl, "POST", System.Text.Encoding.ASCII.GetBytes("field1=value1&field2=value2"));
Run Code Online (Sandbox Code Playgroud)
将所需字段指定为位置参数:
class UpdateScript:
def index(self, field1, field2):
...
Run Code Online (Sandbox Code Playgroud)
或者作为关键字参数:
class UpdateScript:
def index(self, **kwargs):
...
Run Code Online (Sandbox Code Playgroud)
然后,你会得到你想要的.
我用以下python脚本(Python 2.7)测试了它:
import urllib
print urllib.urlopen('http://localhost:8080', 'field1=b&field2=c').read()
Run Code Online (Sandbox Code Playgroud)