1

I want to call app.route method from another app.route method. As an example the code look like this simply. I want to call getMethod() function inside the postMethod() function. How can I do this?

@app.route('/ex1')
def getMethod():
    return "some value"

@app.route('/ex2', methods=['POST'])
def postMethod():
    # want to call getMethod() and get the return value
    # save data to database
    return jsonify('created'), 201
EMYLs
  • 27
  • 7
  • 2
    run it as normal function `getMethod()`. Or code which you need in both functions put in separated function and run this function in `getMethod()` and `postMethod()` – furas Aug 25 '19 at 19:05

1 Answers1

2

Going by the solution here

A possible solution will be to create a normal function without the decorator, then call it at the different places where you want to.

@app.route('/ex1')
def getMethod():
    return commonMethod()

def commonMethod():
    return "some value"

@app.route('/ex2', methods=['POST'])
def postMethod():
    result = commonMethod()
    return jsonify('created'), 201
HAKS
  • 419
  • 4
  • 9