一、Post请求
1、使用python发送一个Post请求
有时候遇到请求url中有很多参数。
1.1 示例1
accounts和pwd请到http://shop-xo.hctestedu.com/注册。
import requests
# 请求体
data = {
"accounts": "xx",
"pwd": "xxx",
"type": "username"
}
# 只有php项目需要application和application_client_type
# 写法一, 在请求Url中带上所有参数,application和application_client_type,用&隔开
response = requests.post(url="http://shop-xo.hctestedu.com/index.php?"
"s=api/user/login"
"&application=app"
"&applicaiton_client_type=weixin", json=data)
# 输出响应结果
print(response.text)
执行结果:
{"msg":"登录成功","code":0,"data":{"id":"7073","username":"xx","nickname":"","mobile":"","email":"","avatar":"http:\/\/shop-xo.hctestedu.com\/static\/index\/default\/images\/default-user-avatar.jpg","alipay_openid":"","weixin_openid":"","weixin_unionid":"","weixin_web_openid":"","baidu_openid":"","toutiao_openid":"","qq_openid":"","qq_unionid":"","integral":"189","locking_integral":"0","referrer":"0","add_time":"1646195490","add_time_text":"2022-03-02 12:31:30","mobile_security":"","email_security":"","user_name_view":"xx","is_mandatory_bind_mobile":0,"token":"xxxxxx"}}
1.2 示例2
使用不定长参数 params,将url中需要的参数单独封装。
import requests
# 请求体
data = {
"accounts": "xx",
"pwd": "xxx",
"type": "username"
}
# 使用不定长参数params
param_data = {
"application": "app",
"application_client_type": "weixin"
}
# 写法2:使用不定长参数params
response = requests.post(url="http://shop-xo.hctestedu.com/index.php?"
"s=api/user/login", params=param_data, json=data)
# 输出响应结果
print(response.text)
执行结果:
{"msg":"登录成功","code":0,"data":{"id":"22299","username":"xx","nickname":"","mobile":"","email":"","avatar":"http:\/\/shop-xo.hctestedu.com\/static\/index\/default\/images\/default-user-avatar.jpg","alipay_openid":"","weixin_openid":"","weixin_unionid":"","weixin_web_openid":"","baidu_openid":"","toutiao_openid":"","qq_openid":"","qq_unionid":"","integral":"0","locking_integral":"0","referrer":"0","add_time":"1678005098","add_time_text":"2023-03-05 16:31:38","mobile_security":"","email_security":"","user_name_view":"xx","is_mandatory_bind_mobile":0,"token":"xxxxx"}}
二、获取Response body
1、response.text
用type()查看response.text的类型,是str
import requests
# 请求体
data = {
"accounts": "xx",
"pwd": "xxx",
"type": "username"
}
# 使用不定长参数params
param_data = {
"application": "app",
"application_client_type": "weixin"
}
# 写法2:使用不定长参数params
response = requests.post(url="http://shop-xo.hctestedu.com/index.php?"
"s=api/user/login", params=param_data, json=data)
# 输出响应结果
print(response.text)
print(type(response.text))
执行结果:
{"msg":"登录成功","code":0,"data":{"id":"22299","username":"xx","nickname":"","mobile":"","email":"","avatar":"http:\/\/shop-xo.hctestedu.com\/static\/index\/default\/images\/default-user-avatar.jpg","alipay_openid":"","weixin_openid":"","weixin_unionid":"","weixin_web_openid":"","baidu_openid":"","toutiao_openid":"","qq_openid":"","qq_unionid":"","integral":"0","locking_integral":"0","referrer":"0","add_time":"1678005098","add_time_text":"2023-03-05 16:31:38","mobile_security":"","email_security":"","user_name_view":"xx","is_mandatory_bind_mobile":0,"token":"xxxxx"}}
<class 'str'>
2、response.json()
用type()查看response.json()的类型,是dict
import requests
# 请求体
data = {
"accounts": "xx",
"pwd": "xxx",
"type": "username"
}
# 使用不定长参数params
param_data = {
"application": "app",
"application_client_type": "weixin"
}
# 写法2:使用不定长参数params
response = requests.post(url="http://shop-xo.hctestedu.com/index.php?"
"s=api/user/login", params=param_data, json=data)
# 输出响应结果
print(response.json())
print(type(response.json()))
执行结果:
{"msg":"登录成功","code":0,"data":{"id":"22299","username":"xx","nickname":"",