這個問題可能在另一端。
使用Tornado 2.4.1進行以下測試會得到預期的輸出。
import logging
import urllib
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler, asynchronous
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from tornado import gen, options
log = logging.getLogger()
options.parse_command_line()
class PutBodyTest(RequestHandler):
@asynchronous
@gen.engine
def get(self):
data = {
'text': 'important text',
'timestamp': 'a timestamp'
}
req = HTTPRequest(
'http://localhost:8888/put_body_test',
method='PUT',
body=urllib.urlencode(data)
)
res = yield gen.Task(AsyncHTTPClient().fetch, req)
self.finish()
def put(self):
log.debug(self.request.body)
application = Application([
(r"/put_body_test", PutBodyTest),
])
if __name__ == "__main__":
application.listen(8888)
IOLoop.instance().start()
日誌輸出:
$ python put_test.py --logging=debug
[D 130322 11:45:24 put_test:30] text=important+text×tamp=a+timestamp
[I 130322 11:45:24 web:1462] 200 PUT /put_body_test (127.0.0.1) 0.37ms
[I 130322 11:45:24 web:1462] 200 GET /put_body_test (::1) 9.76ms
你從哪裏導入'HTTPRequest'?而且你怎麼實例化客戶端' – aychedee 2013-03-21 23:17:39
HTTPRequest來自tornado.httpclient,而客戶端是tornado.httpclient.ASyncHTTPClient的別名。我會更新這個問題來說明問題。 – 2013-03-22 02:18:53
我沒有看到你在這裏的代碼有什麼問題。可能是處理程序代碼中的一個微妙的錯誤? – aychedee 2013-03-22 07:39:30