如何使用python请求转换CURL命令并通过API检索结果

问题描述 投票:0回答:1

我是python的新手,在通过API向服务器请求以下参数时遇到问题。

我正在使用flask,因为我希望它是基于Web的。步骤1:转到index.html并传递teamID teamID = request.form['teamID']。步骤2:通过API对用户进行身份验证并通过teamID查找数据步骤3:转到页面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 syntaxTabError: inconsistent use of tabs and spaces in indentation

谢谢!

python curl flask python-requests flask-restful
1个回答
0
投票

由于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)

可用HERE


为了避免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)
© www.soinside.com 2019 - 2024. All rights reserved.