我正在尝试使用 Tornado ASyncHTTPClient 发出放置请求,如下所示:
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)
但是,当请求到达其所需的端点时,它似乎没有主体,尽管上述主体已在上面正确编码和定义。我在这里忽略了什么吗?
最佳答案
如果另一端需要 JSON,您可能需要设置一个“Content-Type” header 。试试这个:
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)
这样, header 告诉服务器您正在发送 JSON,而正文是一个可以解析为 JSON 的字符串。
关于python - Tornado PUT 请求丢失的正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15534748/