接口测试 Python Requests post 方法中 data 与 json 参数问题

ch · 2019年05月16日 · 835 次阅读

1.data 参数

你想要发送一些编码为表单形式的数据——非常像一个 HTML 表单。要实现这个,只需简单地传递一个字典给 data 参数。你的数据字典在发出请求时会自动编码为表单形式,header 默认 Content-Type: application/x-www-form-urlencoded,

发送过来的抓包到的数据格式为:'username=amy&password=123'

如果你传递一个 string 而不是一个 dict,那么数据会被直接发布出去,不会被编码为表单形式

例如传'{"username": "amy", "password": "123"}',直接发出去,抓包的内容也是'{"username": "amy", "password": "123"}'

2.json 参数

还可以使用 json 参数直接传递,然后它就会被自动编码,header 默认'Content-Type': 'application/json'

json 发送过来的数据格式为:{"username": "amy", "password": "123"}

3.实际场景中如何使用

1)对页面接口抓包,如果 Content-Type:application/json,则限制接受 json 格式,requests 中可用 json 方法,

或者使用 data 方法,headers 加'Content-Type': 'application/json',postdata 传 string 格式,不会自动编码格式

2)对页面接口抓包,Content-Type:application/x-www-form-urlencoded; charset=UTF-8,则可使用 data 方法

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册