今天在用requests模块做api接口的测试,发现传送简单字段,如下所示,后台均能接收成功。
import requests
data = {"version":"1.0.1","collections":[{"type":4,"newsId":1},{"type":1,"newsId":2}]}
requests.post(url='http://xx.com', data=data).text
post请求后,后台只能接收version的参数,而collections的参数接收不正确。
解决方法,加入header头,指定传送参数为json类型,同时将data由字典转为json字符串传送
import requests
import json
headers = {'Content-Type': 'application/json'}
data = {"version":"1.0.1","collections":[{"type":4,"newsId":1},{"type":1,"newsId":2}]}
requests.post(url='http://xx.com', data=json.dumps(data),headers=headers).text