报错:
{'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())
print(para_data) # {'phone': '12345678901', 'password': 'MDAwMDAw'}
print(type(para_data)) # <class 'dict'>
print(demjson3.encode(para_data)) # {"password":"MDAwMDAw","phone":"12345678901"}
print(type(demjson3.encode(para_data))) # <class 'str'>