之所以发不过去是因为请求头不对。发送json需要将请求头 为:
headers={'Content-Type': 'application/json'}
下面的内容不用看了。
昨天晚上和后端对接接口,他告诉我说data string,后端让必须发送表单。给我整懵了。因为我们的测试结果是一个json。
res = requests.post("url",data=json.dumps(result))
这样它不香嘛。
折腾了老半天啊。发现python要发送multipart/form-data数据不能直接用requests.post。需要requests_toolbelt.MultipartEncoder
首先安装:
pip install requests_toolbelt
from requests_toolbelt import MultipartEncoder
data = {
'type':'sdfsd',
'name':'dfg',
'error':'',
'loadtime':'111',
'state':'0',
'date':'123456',
}
n = json.dumps(data)
m = MultipartEncoder(fields={'data': n})
res = requests.post("url",data=m,headers={'Content-Type': m.content_type})