nerdexam
Cisco

350-901 · Question #40

Refer to the exhibit. This snippet of a script has recently started exiting abnormally with an exception stating "Unexpected HTTP Response code: 429". response = requests.get(url) if…

The correct answer is D. response = requests.get(url) if response.status_code == 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) response = requests.get(url) elif response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json(). The script needs to be modified to gracefully handle an HTTP 429 (Too Many Requests) status code, which indicates rate limiting by the API.

Understanding and Using APIs

Question

Refer to the exhibit. This snippet of a script has recently started exiting abnormally with an exception stating "Unexpected HTTP Response code: 429". response = requests.get(url) if response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json() Which solution handles rate limiting by the remote API?

Exhibit

350-901 question #40 exhibit

Options

  • Aresponse = requests.get(url) if response.status_code == 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) elif response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()
  • Bresponse = requests.get(url) if response.status_code != 200 and response.status_code != 429: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()
  • Cresponse = requests.get(url) if response.status_code != 200 and response.status_code != 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()
  • Dresponse = requests.get(url) if response.status_code == 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) response = requests.get(url) elif response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()

How the community answered

(39 responses)
  • A
    8% (3)
  • B
    3% (1)
  • C
    5% (2)
  • D
    85% (33)

Why each option

The script needs to be modified to gracefully handle an HTTP 429 (Too Many Requests) status code, which indicates rate limiting by the API.

Aresponse = requests.get(url) if response.status_code == 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) elif response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()

This option sleeps after receiving a 429 but does not re-issue the 'requests.get(url)' call, meaning 'data = response.json()' would still attempt to parse the original 429 error response.

Bresponse = requests.get(url) if response.status_code != 200 and response.status_code != 429: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()

This option modifies the exception condition to ignore 429 errors but does not implement any backoff or retry logic, leading to the 'data = response.json()' call failing or processing an empty/error response.

Cresponse = requests.get(url) if response.status_code != 200 and response.status_code != 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()

This option incorrectly applies the sleep logic only if the status code is neither 200 nor 429, and then still raises an exception without retrying the request.

Dresponse = requests.get(url) if response.status_code == 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) response = requests.get(url) elif response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()Correct

This solution correctly identifies an HTTP 429 status code, pauses execution for the duration specified in the 'Retry-After' header, and then re-attempts the 'requests.get(url)' call to retrieve the data. This implements an effective rate-limiting retry mechanism, allowing the script to eventually succeed after waiting.

Concept tested: API rate limiting handling with retry logic

Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429

Topics

#API Rate Limiting#HTTP Status 429#Retry Mechanism#Python Requests

Community Discussion

No community discussion yet for this question.

Full 350-901 Practice