Advanced

Forms and redirects

A form is a request built by a browser, and a browser builds it in a shape the code did not choose. The shape is the reason a redirect can lose a method.

What you will see

One line. One canonical path is what removes the added redirect.

About 1 min of reading

Try It

Choose the redirect number

1

Write the two numbers

printf '303 get\n308 keep\n' > /tmp/n.txt; grep keep /tmp/n.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
308 keep. The number is the claim the browser obeys.
2

Count the hops

printf '/ping\n/ping/\n/ping\n' > /tmp/h.txt; wc -l < /tmp/h.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3. Three lines is two hops, and two hops is the cost.
3

Write the slash rule

printf 'rule=one-canonical-path\n' > /tmp/r.txt; cat /tmp/r.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. One canonical path is what removes the added redirect.

Data Flow

A form to a kept method
1 The form posts to a path
↓
2 The path redirects with a number
↓
3 The browser chooses the method
↓
4 The number is chosen to keep it
Why this works
The cost of a chain

Each hop is a round trip. A chain of three hops is three trips, and three trips is a number that shows in the log.

When you have a choice

Why the shape is tested rather than remembered

The browser decides. A test that uses a real browser is a test that reads the decision, and the decision is the truth.

Examples

A redirect after a post is a get

The browser follows with the method it is allowed to use. The post is gone, and the gone is the whole bug.

The answer is a 307 or a 308

They say keep the method. The number is the claim, and the claim is what the browser obeys.

The trailing slash is a redirect

It is a redirect the framework adds. An added redirect is a round trip nobody designed.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Authentication in front of a Flask app
Previous Lesson
Authentication in front of a Flask app
Next Lesson
Serving files safely
Serving files safely →