3

I want to redirect users to same path but with different arguments.

Here's my code:

@app.route('/foo')
def foo(args1=None, args2=None):
    return render_template('foo.html', args1=args1, args2=args2)

@app.route('/bar')
def redirect_to_foo():
    return redirect(url_for('foo', args1='something'))

But when I see url path, that shows /foo?arg1=something... .
How redirect with args but without query string?
Is there any way to do this?

wenzul
  • 3,948
  • 2
  • 21
  • 33
Jony cruse
  • 815
  • 1
  • 13
  • 23
  • See related http://programmers.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect. HTML has no POST redirect. If you will implement it with Status-Code 307 the user will get a prompt. Another way would be to save arguments in a session before sending 301-redirect code. – wenzul Aug 21 '15 at 06:47
  • See also http://stackoverflow.com/questions/2068418/whats-the-difference-between-a-302-and-a-307-redirect. – wenzul Aug 21 '15 at 06:49

1 Answers1

1

In HTTP 1.1, there actually is a status code (307) which indicates that the request should be repeated using the same method and post data. Try to use HTTP status code 307 Internal Redirect instead of 302. For flask there is a parameter code.

@app.route('/bar', methods=['POST'])
def redirect_to_foo():
    return redirect(url_for('foo', args1='something'), code=307)

Source: Redirecting to URL in Flask

And as a background also have a look in my comments in OP.

Hint: Maybe it's not the right I answer. I didn't understand why you want to redirect with different arguments. Sending back "different arguments" as a HTTP payload beside an redirect header is AFAIK (except for GET/"in query string") not possible. If you really have to do it, think about the structure.

Community
  • 1
  • 1
wenzul
  • 3,948
  • 2
  • 21
  • 33