I'm new to python and encountered problems passing the parameters below that I want to request from the server via API.
I'm using flask as I want this to be web based.
Step1: go to index.html and pass teamID teamID = request.form['teamID']
.
Step2: Authenticate user via API & find data by teamID
Step3: Go to the page report.html and display results + save results as CSV file
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"
-----------------
This is CURL command that I would like to convert into python requests format:
#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 & "'"
my code so far:
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)
Could someone help with this? So far I tried the code above but received SyntaxError: invalid syntax
or TabError: inconsistent use of tabs and spaces in indentation
Thanks in advance!
Thanks to TrillWorks, we have now an online curl to python converter.
So for:
curl -X POST -H \"Content-Type: application/json\" -H \"accessToken:" & authToken & "\" \"" & serverURL & "teamID/" & teamID & "/report\" -d $'" & commandBody & "'
You have:
import requests
headers = {
'\\Content-Type': 'application/json" -H "accessToken:',
}
response = requests.post('http://&', headers=headers)
It is available HERE.
To avoid 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)