在最近写的一个RESTful API Server过程中,发现tornaod对解析POST BODY的内容有限制。
设置header的Content-type参数,为 application/x-www-form-urlencoded,否则tornado不会解析request
body中的内容。
put方法,访问指定URL
values = {'user': slice,
'result': 'winning'}
#
jdata = json.dumps(values) # 对数据进行JSON格式化编码
urm = '%s%s' % (SERVER['XV'], '/v1/xxxxx')
request = urllib2.Request(urm, jdata)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
request.get_method = lambda: 'PUT' # 设置HTTP的访问方式
request = urllib2.urlopen(request)
return request.read()
本文介绍了在使用Tornado构建RESTful API时遇到的问题,即Tornado仅在Content-type为application/x-www-form-urlencoded时才会解析POST请求的body内容。同样,对于PUT方法,也需要设置Content-Type header来确保body内容被正确解析。通过设置HTTP请求的方式为PUT,并添加正确的Content-Type,可以成功发送并处理PUT请求。
3128

被折叠的 条评论
为什么被折叠?



