0
0
Rest APIprogramming~20 mins

301 and 302 redirects in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redirect Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:00remaining
What is the HTTP status code returned?

Consider this HTTP response header snippet from a REST API:

HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-location

What status code does this response return?

A302
B200
C301
D404
Attempts:
2 left
💡 Hint

Look at the first line of the HTTP response header.

Predict Output
intermediate
1:30remaining
What happens after a 302 redirect?

A client receives this HTTP response:

HTTP/1.1 302 Found
Location: https://example.com/temporary

What does the client do next?

AIt permanently updates the URL and caches it.
BIt ignores the Location header and stays on the original URL.
CIt returns an error to the user.
DIt follows the new URL temporarily without caching the redirect.
Attempts:
2 left
💡 Hint

Think about the difference between temporary and permanent redirects.

🧠 Conceptual
advanced
2:00remaining
Why choose 301 over 302 for SEO?

Which reason best explains why a website owner should use a 301 redirect instead of a 302 redirect when moving a page permanently?

ABecause 301 redirects preserve search engine rankings by transferring link equity.
BBecause 301 redirects are faster to process by browsers.
CBecause 302 redirects cause the page to load twice.
DBecause 302 redirects are only for images and media files.
Attempts:
2 left
💡 Hint

Think about how search engines treat permanent vs temporary redirects.

Predict Output
advanced
2:00remaining
What is the output of this redirect handling code?

Given this Python Flask code snippet handling redirects:

from flask import Flask, redirect
app = Flask(__name__)

@app.route('/old')
def old():
    return redirect('https://example.com/new', code=302)

with app.test_client() as client:
    response = client.get('/old')
    print(response.status_code, response.headers.get('Location'))

What is printed?

Rest API
from flask import Flask, redirect
app = Flask(__name__)

@app.route('/old')
def old():
    return redirect('https://example.com/new', code=302)

with app.test_client() as client:
    response = client.get('/old')
    print(response.status_code, response.headers.get('Location'))
A404 /old
B302 https://example.com/new
C301 https://example.com/new
D200 None
Attempts:
2 left
💡 Hint

Check the redirect code argument in the redirect function.

🔧 Debug
expert
3:00remaining
Why does this redirect cause a loop?

Consider this pseudo-code for a REST API endpoint:

if request.path == '/start':
    return redirect('/middle', code=302)
elif request.path == '/middle':
    return redirect('/start', code=302)

What is the main reason this code causes an infinite redirect loop?

Rest API
if request.path == '/start':
    return redirect('/middle', code=302)
elif request.path == '/middle':
    return redirect('/start', code=302)
ABecause '/start' redirects to '/middle' and '/middle' redirects back to '/start', causing endless redirects.
BBecause 302 redirects are not supported for these paths.
CBecause the redirect function is missing a status code.
DBecause the server does not handle GET requests properly.
Attempts:
2 left
💡 Hint

Trace the redirects step-by-step.