2013-03-20 164 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)