How to use Flask API to post two variables via Postman and run a function using them in that call?

Issue

I have the following function :
`

def file(DOCname,TABLEid):
 directory = DOCname
 parent_dir = "E:\\Tables\\Documents\\"+TABLEid
 path = os.path.join(parent_dir, directory)

 try:
      os.makedirs(path, exist_ok = True)
      print("Directory '%s' created successfully" % directory)
 except OSError as error:
      print("Directory '%s' can not be created" % directory)

`

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.

Solution

It can be done in following way. The data from the Postman should be send
through the forms .

from flask import Flask
from flask import request
app = Flask(__name__)

@app.route("/",methods=["POST"])
def file():
 dic_data = request.form 
 DOCname= dic_data["DOCname"]
 TABLEid = dic_data["TABLEid"]

 directory = DOCname
 parent_dir = "E:\\Tables\\Documents\\"+TABLEid
 path = os.path.join(parent_dir, directory)

 try:
      os.makedirs(path, exist_ok = True)
      print("Directory '%s' created successfully" % directory)
 except OSError as error:
      print("Directory '%s' can not be created" % directory)

enter image description here

Answered By – Bharat Adhikari

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published