1

I'm making API and looking for a way to hide the extra information from the url. I have a function index:

@app.route('/', methods=['GET', 'POST'])
def index():
    count = foo()
    return redirect(url_for("result", count=count))

and a function result

@app.route("/done/<count>")
def result(count):
    count = count
    return jsonify(count=count)

Inner function count allwase return different values. At the end I get a result like

http://127.0.0.1:5000/done/43

But I need more common url view for universal API like

http://127.0.0.1:5000/done

The problem is that if I remove <count> from endpoint, i get error

TypeError: result() missing 1 required positional argument: 'count'

Is there a way to override this?

Jekson
  • 2,892
  • 8
  • 44
  • 79
  • Delete `count` variable from function definition `def result`. – bc291 Mar 04 '19 at 14:45
  • Do you need to preserve `count` variable value? – bc291 Mar 04 '19 at 14:47
  • If remove `count` from `def result` I get error. The count values is needed to return at json file – Jekson Mar 04 '19 at 14:53
  • 1
    I think that this is not possible then. Basically, you are redirecting to new url, with `count` variable as a part of it. `http://127.0.0.1:5000/done/count` request must be made in order to preserve that value between requests. If you still insists, you may store that value in cookie/session for each user or so. – bc291 Mar 04 '19 at 20:13

1 Answers1

1

This task solving by session variable

from flask import session

@app.route('/', methods=['GET', 'POST'])
def index():
    count = foo()
    session['count'] = count
    return redirect(url_for("result"))

@app.route("/done/")
def result(count):
    count = session['count']
    return jsonify(count=count)
Jekson
  • 2,892
  • 8
  • 44
  • 79