python requests post 发送字典|二维数组接收不到的问题

今天在用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

转载于:https://my.oschina.net/u/3403514/blog/1490513

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值