Bird
Raised Fist0
Rest APIprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does a 301 redirect indicate in a REST API?
easy
A. The resource has been deleted.
B. The resource is temporarily unavailable.
C. The resource has moved permanently to a new URL.
D. The resource requires authentication.

Solution

  1. Step 1: Understand the meaning of 301 redirect

    A 301 redirect means the resource has moved permanently to a new location.
  2. Step 2: Compare with other options

    Other options describe different HTTP statuses, not 301 redirect.
  3. Final Answer:

    The resource has moved permanently to a new URL. -> Option C
  4. Quick Check:

    301 redirect = permanent move [OK]
Hint: 301 means permanent move, 302 means temporary move [OK]
Common Mistakes:
  • Confusing 301 with 302 redirect
  • Thinking 301 means resource deleted
  • Assuming 301 is for authentication
2. Which HTTP status code is used for a temporary redirect?
easy
A. 301
B. 302
C. 404
D. 500

Solution

  1. Step 1: Identify status codes for redirects

    302 is the standard status code for temporary redirects.
  2. Step 2: Eliminate other codes

    301 is permanent redirect, 404 is not found, 500 is server error.
  3. Final Answer:

    302 -> Option B
  4. Quick Check:

    Temporary redirect = 302 [OK]
Hint: 302 means temporary redirect, not 301 [OK]
Common Mistakes:
  • Mixing 301 and 302 codes
  • Choosing 404 or 500 by mistake
  • Not knowing redirect codes
3. What will be the HTTP status code returned by this REST API snippet?
return Response(status=301, headers={'Location': 'https://newsite.com'})
medium
A. 301 Moved Permanently
B. 302 Found
C. 200 OK
D. 404 Not Found

Solution

  1. Step 1: Analyze the status code in the response

    The code explicitly sets status=301, which means permanent redirect.
  2. Step 2: Check the Location header

    The Location header points to the new URL, confirming redirect behavior.
  3. Final Answer:

    301 Moved Permanently -> Option A
  4. Quick Check:

    Status 301 with Location = permanent redirect [OK]
Hint: Status 301 with Location header means permanent redirect [OK]
Common Mistakes:
  • Assuming 200 OK without checking status
  • Confusing 301 with 302
  • Ignoring Location header
4. Identify the error in this code snippet for a temporary redirect:
return Response(status=301, headers={'Location': 'https://temp.example.com'})
medium
A. Location header is missing.
B. Headers should not include Location.
C. Status code 301 is correct for temporary redirect.
D. Status code should be 302 for temporary redirect.

Solution

  1. Step 1: Check status code for temporary redirect

    Temporary redirects require status code 302, not 301.
  2. Step 2: Verify headers

    Location header is present and correct, so no error there.
  3. Final Answer:

    Status code should be 302 for temporary redirect. -> Option D
  4. Quick Check:

    Temporary redirect = 302, not 301 [OK]
Hint: Use 302 status for temporary redirects, not 301 [OK]
Common Mistakes:
  • Using 301 instead of 302 for temporary redirects
  • Omitting Location header
  • Misunderstanding redirect status codes
5. You want to permanently redirect old API endpoints to new ones but keep temporary redirects for beta features. Which status codes should you use for each case?
hard
A. 301 for old endpoints, 302 for beta features
B. 302 for old endpoints, 301 for beta features
C. 404 for old endpoints, 302 for beta features
D. 500 for old endpoints, 301 for beta features

Solution

  1. Step 1: Determine redirect type for old endpoints

    Old endpoints moved permanently, so use 301 status code.
  2. Step 2: Determine redirect type for beta features

    Beta features are temporary, so use 302 status code.
  3. Final Answer:

    301 for old endpoints, 302 for beta features -> Option A
  4. Quick Check:

    Permanent = 301, Temporary = 302 [OK]
Hint: Permanent redirect = 301, temporary redirect = 302 [OK]
Common Mistakes:
  • Swapping 301 and 302 codes
  • Using error codes instead of redirects
  • Not distinguishing permanent vs temporary
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard