2013-03-20 128 views
6

我试图用龙卷风ASyncHTTPClient像这样做PUT请求:龙卷风PUT请求缺少正文

data = { 'text': 'important text', 
      'timestamp': 'an iso timestamp' } 

    request = tornado.httpclient.HTTPRequest(URL, method = 'PUT', body = urllib.urlencode(data)) 

    response = yield Task(tornado.httpclient.ASyncHTTPClient().fetch, request) 

然而,当请求达到其期望的终点,它似乎没有一个机构,尽管所述身体被正确编码并在上面定义。我在这里忽略了什么?


+0

你从哪里导入'HTTPRequest'?而且你怎么实例化客户端' – aychedee 2013-03-21 23:17:39

+1

HTTPRequest来自tornado.httpclient,而客户端是tornado.httpclient.ASyncHTTPClient的别名。我会更新这个问题来说明问题。 – 2013-03-22 02:18:53

+0

我没有看到你在这里的代码有什么问题。可能是处理程序代码中的一个微妙的错误? – aychedee 2013-03-22 07:39:30

回答

4

如果另一端期待JSON,你可能需要设置一个“内容类型”标头。试试这个:

data = { 'text': 'important text', 
     'timestamp': 'an iso timestamp' } 

headers = {'Content-Type': 'application/json; charset=UTF-8'} 

request = tornado.httpclient.HTTPRequest(URL, method = 'PUT', headers = headers, body = simplejson.dumps(data)) 

response = yield Task(tornado.httpclient.ASyncHTTPClient().fetch, request) 

这样一来,头告诉你发送JSON的服务器,和身体是可以被解析为JSON字符串。

+1

这工作,谢谢一堆! – 2013-03-26 00:23:34

2

这个问题可能在另一端。
使用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&timestamp=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 
+1

谢谢!我已经开始怀疑类似的情况,运行这个测试用例帮助证实了这一点。 – 2013-03-25 22:09:06

0

这是预期JSON的发布请求!试试这个:

@gen.coroutine 
    def post(self): 
     http_client = AsyncHTTPClient() 
     http_client = tornado.httpclient.AsyncHTTPClient() 

     URL = "http://localhost:1338/api/getPositionScanByDateRange" 
     data = {"startDate":"2017-10-31 18:30:00","endDate":"2018-02-08 12:09:14","groupId":3} #A dictionary of your post data 
     headers = {'Content-Type': 'application/json; charset=UTF-8'} 

     record = yield http_client.fetch(URL, method = 'POST', headers = headers, body = json.dumps(data)) 

     if record.error: 
      response = record.error 
     else: 
      response = record.body 
     self.set_header('Content-Type', 'application/json') 
     self.finish(response)