Python 3.x 在猎鹰中如何将身体发送到模拟哨所
我从Falcon服务器收到json解码错误 我已经实现了一个中间件来解码jsonPython 3.x 在猎鹰中如何将身体发送到模拟哨所,python-3.x,testing,falconframework,Python 3.x,Testing,Falconframework,我从Falcon服务器收到json解码错误 我已经实现了一个中间件来解码json def test_with_invalid_param(self): body = urlencode({ "email":"jit", "password":"ewr" }) result = self.simulate_post(self.path, body=body, headers=self.headers)
def test_with_invalid_param(self):
body = urlencode({
"email":"jit",
"password":"ewr"
})
result = self.simulate_post(self.path, body=body, headers=self.headers)
print(result.json)
self.assertEqual(result.status, "201 OK")
有人能发现这里出了什么问题吗?由此看来,您需要设置内容类型:
try:
req.data = json.loads(req.stream.read().decode("utf-8"))
return
except:
raise falcon.HTTPBadRequest(
"Bad request", "Invalid body. Unable to parse the given content"
)
尝试使用有界流:
您的代码如下所示:
req.data=json.load(req.bounded\u stream.read().decode(“utf-8”))
我仍然收到中间件异常“请求错误”、“正文无效。无法解析给定内容”
body = urlencode({
'username': 'new_user',
'email': 'email',
'password': 'password',
'location': 'Tucson, AZ'
})
headers = {"Content-Type": "application/x-www-form-urlencoded"}
self.simulate_post(self.entry_path, body=body, headers=headers)