I have the following function : `
JavaScript
x
11
11
1
def file(DOCname,TABLEid):
2
directory = DOCname
3
parent_dir = "E:\Tables\Documents\"+TABLEid
4
path = os.path.join(parent_dir, directory)
5
6
try:
7
os.makedirs(path, exist_ok = True)
8
print("Directory '%s' created successfully" % directory)
9
except OSError as error:
10
print("Directory '%s' can not be created" % directory)
11
`
Now I want to use a Flask API and call this function to run with two variables that I will provide via Postman, DOCname and TABLEid, but I’m not sure how to run this at the same time I make an API call ?
I tried to run the file under a post request but nothing seems to happen.
Advertisement
Answer
It can be done in following way. The data from the Postman should be send through the forms .
JavaScript
1
20
20
1
from flask import Flask
2
from flask import request
3
app = Flask(__name__)
4
5
@app.route("/",methods=["POST"])
6
def file():
7
dic_data = request.form
8
DOCname= dic_data["DOCname"]
9
TABLEid = dic_data["TABLEid"]
10
11
directory = DOCname
12
parent_dir = "E:\Tables\Documents\"+TABLEid
13
path = os.path.join(parent_dir, directory)
14
15
try:
16
os.makedirs(path, exist_ok = True)
17
print("Directory '%s' created successfully" % directory)
18
except OSError as error:
19
print("Directory '%s' can not be created" % directory)
20