1:
r = requests.post(url="http://127.0.0.1:8000/test/",
data={'k':'k1'}
)
#请求头application/x-www-form-urlencoded ----> 用request.POST.get('k')接收数据
2: json数据的话,requests内部会自动帮助序列化数据---> json.dumps() --> 转换成unicode
r = requests.post(url="http://127.0.0.1:8000/test/",
json={'k':'k1'}
)
#请求头application/json ---> 用request.body获取数据
3: #json={'k':'中间'} 其实会在内部序列化之后的字符串传递给data
r = requests.post(url="http://127.0.0.1:8000/test/",
json={'k':'中间'}
)
#请求头application/json ---> 用request.body获取数据 ---> json.loads(数据)
4: #涉及到中文的时候需要字符转转成bytes,内部会检测,发现如果是bytes; requests 就不处理字节了,直接发送数据
r = requests.post(url="http://127.0.0.1:8000/test/",
data=bytes(json.dumps({'k':'中间'},ensure_ascii=False),encoding="utf-8")
)
#请求头application/json ---> 用request.body获取数据 ---> 数据.decode("utf-8") 回来
5:
浏览器请求内容:
FromData:
r = requests.post(url="http://127.0.0.1:8000/test/",
data={'k':'k1'}
)
Payload:
r = requests.post(url="http://127.0.0.1:8000/test/",
json={'k':'k1'}
)
#json内容中文的话:
r = requests.post(url="http://127.0.0.1:8000/test/",
data=bytes(json.dumps({'k':'中间'},ensure_ascii=False),encoding="utf-8")
)
6:补充JSON内容
data = {
'k1':'k1',
'k2':'可以'
}
print(json.dumps(data)) #{"k1": "k1", "k2": "\u53ef\u4ee5"} unicode编码了
data1 = {
'k1':'k1',
'k2':'可以'
}
print(json.dumps(data1,ensure_ascii=False)) #{"k1": "k1", "k2": "可以"}