【发布时间】:2016-07-24 18:11:49
【问题描述】:
网址是http://*.*.*.*/100/?id=1&version=1
参数是
{"cityId": "110000", "query": {"queryStr": "line1", "queryExp": ""}, "channelId": "house"}
卷曲命令是:
curl -X POST -H "Content-Type: application/json" -d '{"cityId": "110000", "query": {"queryStr": "line1", "queryExp": ""}, "channelId": "house"}' "http://*.*.*.*/100/?id=1&version=1"
但是当我使用 tornado(4.2) AsyncHTTPClient 时,我得到了错误:
tornado.application:Future exception was never retrieved: Traceback (most recent call last):
...
HTTPError: HTTP 500: Internal Server Error
我这样要求:
@gen.coroutine
def request(self, url, method="GET", headers=None, data=None):
logger.debug(method)
logger.debug(headers)
logger.debug(data)
headers = {
'content-type': 'application/json',
}
req = HTTPRequest(
url,
method=method,
headers=headers,
body=urllib.urlencode(data).encode('utf-8')
)
http_response = yield self.r.fetch(
# req,
# self.handle_request
url,
method=method,
headers=headers,
body=urllib.urlencode(data).encode('utf-8')
)
# logger.debug(http_response)
raise gen.Return(json.loads(http_response.body))
【问题讨论】:
-
你怎么称呼这个
request函数?你在 python 代码中有method="GET",但在 curl 命令行中有-X POST。 -
我称它为这样的函数(方法等于“POST”):
body = yield r.request( url, method=doc["method"], headers=doc["headers"], data=doc["data"] )