Bird
0
0

Which Python code snippet correctly retries a REST API GET request up to 3 times and stops if successful?

easy📝 Syntax Q3 of 15
Rest API - Webhooks and Events

Which Python code snippet correctly retries a REST API GET request up to 3 times and stops if successful?

import requests

Afor attempt in range(3): response = requests.post('https://api.example.com') if response.status_code == 200: continue
Bwhile True: response = requests.get('https://api.example.com') if response.status_code != 200: break
Cfor i in range(1, 4): response = requests.get('https://api.example.com') if response.status_code == 500: break
Dfor attempt in range(3): response = requests.get('https://api.example.com') if response.status_code == 200: break
Step-by-Step Solution
Solution:
  1. Step 1: Loop 3 times

    Use a for loop with range(3) to limit retries.
  2. Step 2: Make GET request

    Use requests.get() to call the API.
  3. Step 3: Check success

    If status_code is 200, break the loop to stop retrying.
  4. Final Answer:

    for attempt in range(3): response = requests.get('https://api.example.com') if response.status_code == 200: break correctly implements this logic.
  5. Quick Check:

    Loop 3 times, break on success [OK]
Quick Trick: Use for loop with break on status 200 [OK]
Common Mistakes:
MISTAKES
  • Using while True without break condition
  • Checking wrong status codes for success
  • Using POST instead of GET for retrieval

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes