2015-08-27 100 views
0

我已经编写了一个服务,它将json作为输入。我正在使用网站hurl.it发送帖子请求进行检查。下面是我的代码片段:Tornado POST请求未检测到作为参数的json输入

class BatchSemanticSimilarityHandler(tornado.web.RequestHandler): 
def post(self): 
    self.set_header('Access-Control-Allow-Origin', '*') 
    self.set_header('Access-Control-Allow-Credentials', 'true') 
    self.set_header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') 
    self.set_header('Access-Control-Allow-Headers','Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token') 
    data = json.loads(self.request.body) 
    apikey = data["apikey"] 
    try: 
     UA = self.request.headers["User-Agent"] 
    except: 
     UA = "NA" 
    if bool(usercoll.find_one({"apikey":apikey})) == True: 
     sentence = data["sentence"] 
     sentence_array = data["sentence_array"] 
     n = data["num_of_results"]   
     if sentence is None or sentence_array is [] or apikey is None or n is None: 
      self.set_status(200) 
      output = {"error":[{"code":334,"message":"Bad Input data"}]} 
      misscoll.insert({"apitype":"batchsemanticsimilarity","timestamp":datetime.datetime.now(), "ip":self.request.remote_ip, "useragent":UA, "uri":self.request.uri,"apikey":apikey, "output":output, "input":{"s1":sentence,"s2":sentence_array}}) 
      self.write(output) 
      return 
     results = nb.get_similar(sentence, sentence_array, apikey, n) 
     print "results is",results 
     output = {"similar_sentences": results, 'credits':'ParallelDots'} 
     hitscoll.insert({"apitype":"batchsemanticsimilarity","timestamp":datetime.datetime.now(), "ip":self.request.remote_ip, "useragent":UA, "uri":self.request.uri,"apikey":apikey, "output":output, "input":{"s1":sentence,"s2":sentence_array}}) 
     self.write(output) 
     return 
    else: 
     rejectcoll.insert({"apitype":"batchsemanticsimilarity","apikey":apikey,"timestamp":datetime.datetime.now(), "ip":self.request.remote_ip, "useragent":UA, "url":self.request.uri}) 
     self.write({"error":[{"code":333,"message": "Bad Authentication data"}]}) 
     return 

,我给作为请求的主体是下面的JSON:

{ 
"sentence": "BJP leads in Bengaluru civic body`s poll, all eyes on JD(S)", 
"sentence_array": [ 
    "Narendra Modi is the prime minister", 
    "Sonia Gandhi runs Congress", 
    "Sachin is a good batsman" 
], 
"apikey": "DyMe1gSNhvMV1I1b20a7KARYIwuQX5GAQ", 
"num_of_results": 2 
} 

我已经在jsonlint证实,这是一个有效的JSON。 但是发送请求它给了我下面的错误:

ValueError: No JSON object could be decoded 

谁能帮我整理了这一点!

回答

0

您在POST请求中传递的JSON对象被编码到url中。 JSON库无法读取编码的数据。因此,您需要先解码网址。 url的解码可以使用urlparse库在python.so中完成,你需要这样的东西。 post_data=urlparse.parse_qsl(self.request.body)

根据您的需要最终格式的阅读中有urlparse.check各种方法this

如文档指定您可以覆盖的方法,使JSON解析

def prepare(self): if self.request.headers["Content-Type"].startswith("application/json"): self.json_args = json.loads(self.request.body) else: self.json_args = None

检查this

+0

问题已解决!上面提到的解决方案并不是这里的问题。 json被正确解析,因为我从本地检查了相同的代码。这个问题是由于服务器上的端口封闭,我试图通过nginx打到 – jyoti

+0

@jyoti如果你之前已经添加了这个,检测它是服务器问题会很容易。我去解析问题看到错误。Anyway如果它的服务器关闭端口问题,为什么会有解析错误? –