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?

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I tried 307 redirect but the url redirected is still /foo?args=something .
@Jonycruse It will work. Your initial request also have to be a POST request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.