Given this HTTP response header string, what will the printed output be?
headers = {
'X-RateLimit-Limit': '1000',
'X-RateLimit-Remaining': '250',
'X-RateLimit-Reset': '1672531200'
}
limit = int(headers.get('X-RateLimit-Limit', 0))
remaining = int(headers.get('X-RateLimit-Remaining', 0))
reset = int(headers.get('X-RateLimit-Reset', 0))
print(f"Limit: {limit}, Remaining: {remaining}, Reset: {reset}")Check how the get method retrieves values and the default values used.
The code extracts the three rate limit headers and converts them to integers. Since all headers exist, the values are parsed correctly.
In REST APIs, what does the X-RateLimit-Reset header usually indicate?
Think about what a timestamp represents compared to a duration.
X-RateLimit-Reset typically gives the exact time (UNIX timestamp) when the rate limit window resets, not a count or duration.
Consider this code snippet that parses rate limit headers:
limit = int(headers['X-RateLimit-Limit']) remaining = int(headers['X-RateLimit-Remaining']) reset = int(headers['X-RateLimit-Reset'])
Sometimes it raises ValueError. Why?
Check what happens if the header value is not a valid number string.
If a header value is an empty string or contains letters, int() raises ValueError. This is common if the server sends malformed headers.
You want to add rate limit headers to a Flask HTTP response. Which code snippet is correct?
from flask import Flask, jsonify app = Flask(__name__) @app.route('/') def index(): response = jsonify(message='Hello') # Add headers here return response
Check how to assign headers in Flask response objects.
Flask response headers are a dictionary-like object. You assign string values by key. Methods like set_header or add do not exist. Replacing the entire headers dict is not allowed.
An API returns these headers on each call:
X-RateLimit-Limit: 10 X-RateLimit-Remaining: 7 X-RateLimit-Reset: 1700000000
You make 3 calls in a row, each returning the same headers but with X-RateLimit-Remaining decreasing by 1 each time. What is the X-RateLimit-Remaining value after the third call?
Subtract one remaining request per call from the initial remaining count.
Starting with 7 remaining, after 3 calls each reducing remaining by 1, the remaining is 7 - 3 = 4.