python - 如何使用 python 请求库发送此请求
问题描述
如何使用 python 请求库发送以下请求?
要求 :
我努力了
with requests.Session() as session:
// Some login action
url = f'http://somewebsite.com/lib/ajax/service.php?key={key}&info=get_enrolled'
json_data = {
"index": 0,
"methodname": "get_enrolled",
// And so on, from Request Body
}
r = session.post(url, json=json_data)
但它没有给出我想要的输出。
解决方案
1.定义一个POST请求方法
import urllib3
import urllib.parse
def request_with_url(url_str, parameters=None):
"""
https://urllib3.readthedocs.io/en/latest/user-guide.html
"""
http = urllib3.PoolManager()
response = http.request("POST",
url_str,
headers={
'Content-Type' : 'application/json'
},
body=parameters)
resp_data = str(response.data, encoding="utf-8")
return resp_data
2.使用您的特定网址和参数调用该函数
json_data = {
"index": 0,
"methodname": "get_enrolled",
// And so on, from Request Body
}
key = "123456"
url = "http://somewebsite.com/lib/ajax/service.php?key={0}&info=get_enrolled".format(key)
request_with_url(url, json_data)
推荐阅读
- symfony4 - Symfony 4.2:LexikJWT:找不到路径“/api/login_check”的控制器。路由配置错误
- android - 具有无线网络配置的 NFC 标签不适用于 Android Pie
- xaml - I need code behind version of Image source binding
- android - Room:在函数体中抓取dao选中的数据,经过额外处理后返回
- python - 如果键在列表中,如何更新嵌套字典中的值?
- google-cloud-platform - 对于带别名的 GKE Kubernetes 集群,自定义辅助范围名称默认为什么?
- python - 如何从您制作的列表中重新排列单词中的字母?
- azure - 是否可以在 Azure DevOps 中链接到文件的原始内容
- c# - DryIoc 构造函数参数重用
- c# - 让模型以 3D 方式夹在相机上