python - 如何使用 python 请求转换 CURL 命令并通过 API 检索结果
问题描述
我是 python 新手,在传递下面我想通过 API 从服务器请求的参数时遇到了问题。
我正在使用烧瓶,因为我希望它是基于网络的。第 1 步:转到 index.html 并传递 teamID teamID = request.form['teamID']
。Step2:通过API验证用户并通过teamID查找数据Step3:进入页面report.html并显示结果+将结果保存为CSV文件
import subprocess
import json
import requests
from flask import Flask, render_template, request
#required parameters
myID = 12345
teamID = 3456
includeSub = 0
beginDate = "2020-03-20 00:00:00"
endDate = "2020-03-22 00:00:00"
Type = 1
authToken = "1245dgfdfg655432gf"
serverURL = https://myurl.com/env/re/
#optional parameters
Category = 1
fpDate = 1
versionP = "1.0"
-----------------
这是我想转换成 python 请求格式的 CURL 命令:
#Curl command that i need to covert to python requests format
"curl -X POST -H \"Content-Type: application/json\" -H \"accessToken:" & authToken & "\" \"" & serverURL & "teamID/" & teamID & "/report\" -d $'" & commandBody & "'"
到目前为止我的代码:
app = Flask(__name__)
@app.route('/reports', methods=['POST'])
def getmyTeamReport(authToken, myID):
teamID = request.form['teamID']
headers = { 'accept': 'application/json',
'Content-Type': 'application/json',
'accessToken': authToken,
'userID': myID}
commandBody = {
"teamID": teamID,
"includeSub": includeSub,
"beginDate": beginDate,
"endDate": endDate,
"Category": Category,
"fpDate": fpDate,
"versionP": versionP
}
endpoint ='/report'
r = requests.get(serverURL+teamID+endpoint+headers=headers+commandBody=commandBody)
json_object = r.json()
report_t = json_object
return render_template('report.html', report=report_t)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
有人可以帮忙吗?到目前为止,我尝试了上面的代码但收到SyntaxError: invalid syntax
或TabError: inconsistent use of tabs and spaces in indentation
提前致谢!
解决方案
感谢TrillWorks,我们现在有了一个在线 curl 到 python 的转换器。
因此对于:
curl -X POST -H \"Content-Type: application/json\" -H \"accessToken:" & authToken & "\" \"" & serverURL & "teamID/" & teamID & "/report\" -d $'" & commandBody & "'
你有:
import requests
headers = {
'\\Content-Type': 'application/json" -H "accessToken:',
}
response = requests.post('http://&', headers=headers)
它可以在这里找到。
为了避免TabError
:
app = Flask(__name__)
@app.route('/reports', methods=['POST'])
def getmyTeamReport(authToken, myID):
teamID = request.form['teamID']
headers = { 'accept': 'application/json', 'Content-Type': 'application/json','accessToken': authToken,'userID': myID}
commandBody = {"teamID": teamID,"includeSub": includeSub,"beginDate": beginDate,"endDate": endDate,"Category": Category,"fpDate": fpDate,"versionP": versionP}
endpoint ='/report'
r = requests.get(serverURL+teamID+endpoint+headers=headers+commandBody=commandBody)
json_object = r.json()
report_t = json_object
return render_template('report.html', report=report_t)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
推荐阅读
- apache - 通过多个连接使用 Apache Directory LDAP API 对 OpenLDAP 进行分页
- python - 我怎么知道 python 核心是如何工作的?
- java - 如果 Spring Boot 微服务失去 MSSQL DB 连接,如何实现电子邮件/文本通知
- python - Python 线程仅在从另一个模块调用时才有效
- python - 如何在烧瓶响应中返回分块的二进制数据?
- java - 是否可以在 MR 作业之外将数据写入 HDFS 并仍然使用 Hive 进行查询?
- java - java.lang.OutOfMemoryErrorPyRaisable: java.lang.OutOfMemoryError: Java heap space 运行 jupyter notebook 时如何解决?
- javascript - 如何在更改输入时产生过渡效果
- git - 无法解决与 git 子模块文件夹的合并冲突
- python - 将特殊字符写入 csv 文件时出现问题