報錯:
{'msg': "JSON parse error: Unrecognized token 'phone': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'phone': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (PushbackInputStream); line: 1, column: 7]", 'code': '900', 'data': None}
原因:post請求傳遞的JSON數據格式不規范導致的
解決方法:引入demjson轉化一下json格式
安裝:pip install demjson3
import demjson3
url = 'login'
header = {'content-type': 'application/json'}
para_data = {
"phone": "12345678901",
"password": "MDAwMDAw"
}
# data = rest.post(url, data=para_data, headers=header)
# demjson3.encode(object)把對象轉換成json字符串;demjson3.decode(str)把json字符串變json對象
data = rest.post(url, data=demjson3.encode(para_data), headers=header)
print(data.json())
原因:post請求傳遞的JSON數據格式不規范導致的
解決方法:引入demjson轉化一下json格式
安裝:pip install demjson3
python版本要求3.6、3.7、3.8版本,3.9會報錯