0
0
Rest APIprogramming~10 mins

301 and 302 redirects in Rest API - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - 301 and 302 redirects
Client sends HTTP request
Server checks resource location
Resource moved
Send 301 Redirect
Client updates URL
Client requests new URL
When a client requests a URL, the server can respond with a 301 or 302 redirect to tell the client the resource has moved permanently or temporarily, guiding the client to the new URL.
Execution Sample
Rest API
GET /old-page HTTP/1.1
Host: example.com

HTTP/1.1 301 Moved Permanently
Location: /new-page

GET /old-temp HTTP/1.1
Host: example.com

HTTP/1.1 302 Found
Location: /temp-page
This shows a client requesting two URLs and the server responding with 301 and 302 redirects pointing to new locations.
Execution Table
StepRequest URLServer Response CodeLocation HeaderClient Action
1/old-page301/new-pageUpdate URL to /new-page and request it
2/new-page200-Display content of /new-page
3/old-temp302/temp-pageRequest /temp-page but keep /old-temp as original URL
4/temp-page200-Display content of /temp-page
💡 After final 200 OK response, client displays the requested content.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
Requested URL/old-page/new-page/temp-page/temp-page
Response Code-301302200
Location Header-/new-page/temp-page-
Key Moments - 2 Insights
Why does the client update the URL after a 301 but not after a 302?
Because 301 means the resource moved permanently, so the client updates the URL (see execution_table step 1). For 302, the move is temporary, so the client requests the new URL but keeps the original URL for future requests (see step 3).
What happens if the client ignores the Location header?
The client will not request the new URL and will not get the updated resource, so it may show an error or old content. The Location header tells the client where to go next (see execution_table Location Header column).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the client’s requested URL after step 1?
A/old-page
B/new-page
C/old-temp
D/temp-page
💡 Hint
Check the 'Requested URL' variable after step 1 in variable_tracker.
At which step does the client receive a 302 redirect?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Server Response Code' column in execution_table.
If the server changed the 302 to a 301 in step 3, what would the client do differently?
AKeep requesting /old-temp URL
BUpdate URL to /temp-page permanently
CIgnore the Location header
DRequest /new-page instead
💡 Hint
Recall the difference between 301 and 302 redirects explained in key_moments.
Concept Snapshot
301 and 302 Redirects:
- 301 = Moved Permanently: client updates URL permanently.
- 302 = Found (Temporary): client uses new URL temporarily.
- Server sends Location header with new URL.
- Client follows Location to get resource.
- 301 affects bookmarks and SEO; 302 does not.
Full Transcript
When a client requests a web resource, the server can respond with a redirect status code. A 301 redirect means the resource has moved permanently, so the client updates its stored URL and requests the new location. A 302 redirect means the resource is temporarily at a new location, so the client requests the new URL but keeps the original URL for future requests. The server includes a Location header with the new URL in both cases. The client follows this header to get the resource. This process helps keep links working when websites change structure.