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