2013-03-20 9 views
6
लापता अनुरोध

मैं बहुत की तरह एक बवंडर ASyncHTTPClient का उपयोग कर एक निराली अनुरोध करने के लिए कोशिश कर रहा हूँ:तूफान 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

+1

HTTPRequest को तत्काल कैसे कर रहे हैं tornado.httpclient से आ रहा है, और क्लाइंट tornado.httpclient.ASyncHTTPClient के लिए उपनाम है। मैं स्पष्ट करने के लिए सवाल अपडेट करूंगा! –

+0

मुझे आपके पास मौजूद कोड के साथ कुछ भी गलत नहीं दिख रहा है। हैंडलर कोड में एक सूक्ष्म बग हो सकता है? – aychedee

उत्तर

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

यह काम करता है, एक गुच्छा धन्यवाद! –

2

समस्या शायद दूसरे छोर पर है।
टोरनेडो 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

धन्यवाद! मुझे समान संदेह करना शुरू हो गया है, और इस टेस्ट केस को चलाने में उसकी पुष्टि करने में मदद मिली है। –

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)