cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-58546: Adds documentation on how to handle redirects with urllib

Open nezhar opened this issue 1 year ago • 2 comments

Adds an example in the documentation of urllib on how to handle redirects and forward request data.

  • Issue: gh-58546

📚 Documentation preview 📚: https://cpython-previews--121681.org.readthedocs.build/

nezhar avatar Jul 13 '24 10:07 nezhar

All commit authors signed the Contributor License Agreement.
CLA signed

ghost avatar Jul 13 '24 10:07 ghost

In order to test this behavior I implemented a minimal Flask app that implements some endpoints that are doing redirects:

import os
from flask import Flask, redirect, request

app = Flask(__name__)


@app.route('/redirect', methods=['POST', 'GET'])
def redirect_entrypoint():
    return redirect("/redirect-1", code=307)


@app.route('/redirect-1', methods=['POST', 'GET'])
def redirect_first():
    return redirect("/redirect-2", code=307)


@app.route('/redirect-2', methods=['POST', 'GET'])
def redirect_second():
    return redirect("/redirect-final", code=307)


@app.route('/redirect-final', methods=['POST', 'GET'])
def redirect_final():
    try:
        data = request.json
    except:
        data = request.form

    return {
        "message": "Hello, World!",
        "request_data": data
    }


if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

nezhar avatar Jul 13 '24 13:07 nezhar