POST方法传参data与json的区别

这篇博客探讨了在Python的requests库中使用POST方法时,data和json参数的区别。data参数通常需要先用json.dumps将字典转成json字符串,而json参数则会自动完成这个转换。通过实例展示了如何将字典转换为json字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POST方法传参data与json的区别

在通过requests.post()进行POST请求时,传入报文的参数有两个,一个是data,一个是json。

def post(url, data=None, json=None, **kwargs):
  • data与json的区别
    1.data参数需要使用json.dumps将字典类型转换成json格式的字符串对象
    2.json参数会自动将字典类型的对象转换为json格式

    ◆data:字典对象
    ◆json:json字符串

  • 如何将字典对象转换成json字符串?
    1.导入json
    2.Json.dumps(字典对象) #转换json字符串

  • 实例`

在这里插入代码片#1.导包
import requests
import json

#2.调用post
#请求url
url="https://10.65.6*.21*/api/krosa/krosa/auth/login/"
#请求headers
headers={
        "Content-Type":"application/json"
}
#请求json
data={"login_name": "***","password": "***"}
#1-data参数需要使用json.dumps将字典类型转换成json格式的字符串对象
r1=requests.post(url,data=json.dumps(data),headers=headers,verify=False)

#2-json参数会自动将字典类型的对象转换为json格式
r2=requests.post(url,json=data,headers=headers,verify=False)
#3.获取响应对象
print(r1.json())
print(r2.json())
print(type(data))
#4.获取响应状态码
print(r1.status_code)
print(r2.status_code)

响应

D:\python3.6\python.exe F:/python_study/study/test04_post请求(参数data与json区别).py
D:\python3.6\lib\site-packages\urllib3\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host '10.65.66.211'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning,
D:\python3.6\lib\site-packages\urllib3\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host '10.65.66.211'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning,
{'status': 2000, 'message': 'need update', 'result': {'app_name': '运维门户', 'binded': [], 'customer_status': True, 'last_ip': '10.65.66.102', 'app_id': 'tembin', 'create_time': '1601022572', 'partner': None, 'account_type': 'user', 'contact_name': 'tenant_2h', 'partner_id': None, 'account_name': 'tenant_2h', 'customer_name': '666_tx', 'original_ids': [], 'user_id': 'af8e4d59-64e0-3cde-973e-3277d2cdc955', 'parent_id': None, '_login_name': 'tenant_2h', 'op_user_id': '', 'last_login': 1605693183, 'formal': True, 'login_info_enable': 0, 'enable': True, 'first_time': 0, 'account_id': 'af8e4d59-64e0-3cde-973e-3277d2cdc955', 'account_source': 'pamc', 'account_role': 'customer_user', 'avatar_uploaded': False, 'login_source': 'web', 'desc': '{"last_ip": "10.65.66.102", "last_login": 1605693183}', 'remark': '', 'customer_id': '995c1cdd-643a-316e-b1e9-263fffd59b24', 'contact_type': 'customer', 'login_info': {}, 'need_update': True, 'session_id': 'zp8ten3gjw60qjvythe1t3t1l1mib3pm', 'login_name': 'tenant_2h', 'customer_type': 8, 'yichuang_id': None, 'need_verify_code': 0, 'operation_mode': 0}, 'module': 'krosa'}
{'status': 2000, 'message': 'need update', 'result': {'app_name': '运维门户', 'binded': [], 'customer_status': True, 'last_ip': '10.65.66.102', 'app_id': 'tembin', 'create_time': '1601022572', 'partner': None, 'account_type': 'user', 'contact_name': 'tenant_2h', 'partner_id': None, 'account_name': 'tenant_2h', 'customer_name': '666_tx', 'original_ids': [], 'user_id': 'af8e4d59-64e0-3cde-973e-3277d2cdc955', 'parent_id': None, '_login_name': 'tenant_2h', 'op_user_id': '', 'last_login': 1605693184, 'formal': True, 'login_info_enable': 0, 'enable': True, 'first_time': 0, 'account_id': 'af8e4d59-64e0-3cde-973e-3277d2cdc955', 'account_source': 'pamc', 'account_role': 'customer_user', 'avatar_uploaded': False, 'login_source': 'web', 'desc': '{"last_ip": "10.65.66.102", "last_login": 1605693184}', 'remark': '', 'customer_id': '995c1cdd-643a-316e-b1e9-263fffd59b24', 'contact_type': 'customer', 'login_info': {}, 'need_update': True, 'session_id': '56axsag4cc07w60u6hrasgek6p46qoy0', 'login_name': 'tenant_2h', 'customer_type': 8, 'yichuang_id': None, 'need_verify_code': 0, 'operation_mode': 0}, 'module': 'krosa'}
<class 'dict'>
200
200

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值