我有一个web.py服务器。
我处理这样的帖子请求:
class Endpoint(object):
def POST(self):
received_payload = web.data()
# Do things with the payload (a JSON) --- I receive the payload correctly
response = {"Response": "Good"}
# I also tried returning the dictionary directly and building my own requests.Response object
return json.dumps(response)
我能够正确接收和处理我发送给服务器的有效负载,但是我没有收到响应。
在客户端,我这样做:
s = requests.Session()
s.headers.update({ 'Content-Type': 'application/json' })
response = s.post(url=url, json=payload)
请求到达服务器,并正确传递有效负载。但是,我收到了这个错误:
response = s.post(url=url, json=payload)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 511, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 426, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))
我尝试了第一个链接中建议的方法,但是我得到了同样的错误。这可能是因为我使用的是Python 2.7,而原始帖子是关于Python 3.我也试过,基于第二个链接,在web.ctx
中设置输出字段但是,我得到了同样的错误。
将JSON作为对POST请求的响应返回的正确方法是什么?