0
0
Rest APIprogramming~20 mins

Rate limit headers (X-RateLimit) in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rate Limit Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this rate limit header parsing code?

Given this HTTP response header string, what will the printed output be?

Rest API
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}")
ALimit: 1000, Remaining: 0, Reset: 1672531200
BLimit: 1000, Remaining: 250, Reset: 1672531200
CLimit: 0, Remaining: 0, Reset: 0
DLimit: 1000, Remaining: 250, Reset: 0
Attempts:
2 left
💡 Hint

Check how the get method retrieves values and the default values used.

🧠 Conceptual
intermediate
1:30remaining
What does the X-RateLimit-Reset header represent?

In REST APIs, what does the X-RateLimit-Reset header usually indicate?

AThe time when the rate limit window resets, usually as a UNIX timestamp
BThe total number of allowed requests in the current window
CThe duration in seconds until the rate limit resets
DThe number of requests remaining before the limit is reached
Attempts:
2 left
💡 Hint

Think about what a timestamp represents compared to a duration.

🔧 Debug
advanced
2:00remaining
Why does this code raise a ValueError when parsing rate limit headers?

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?

AThe headers dictionary is immutable, so int() cannot convert values
BHeaders might be missing, causing a KeyError, not ValueError
CHeader values might be empty strings or non-numeric, causing int() to fail
DThe int() function cannot convert string numbers to integers
Attempts:
2 left
💡 Hint

Check what happens if the header value is not a valid number string.

📝 Syntax
advanced
2:30remaining
Which option correctly sets rate limit headers in a Flask response?

You want to add rate limit headers to a Flask HTTP response. Which code snippet is correct?

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

@app.route('/')
def index():
    response = jsonify(message='Hello')
    # Add headers here
    return response
A
response.headers['X-RateLimit-Limit'] = '500'
response.headers['X-RateLimit-Remaining'] = '100'
response.headers['X-RateLimit-Reset'] = '1672531200'
B
response.set_header('X-RateLimit-Limit', '500')
response.set_header('X-RateLimit-Remaining', '100')
response.set_header('X-RateLimit-Reset', '1672531200')
C
response.headers.add('X-RateLimit-Limit', 500)
response.headers.add('X-RateLimit-Remaining', 100)
response.headers.add('X-RateLimit-Reset', 1672531200)
D
response.headers = {
    'X-RateLimit-Limit': 500,
    'X-RateLimit-Remaining': 100,
    'X-RateLimit-Reset': 1672531200
}
Attempts:
2 left
💡 Hint

Check how to assign headers in Flask response objects.

🚀 Application
expert
3:00remaining
How many requests remain after this sequence of API calls with rate limit headers?

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?

A0
B7
C10
D4
Attempts:
2 left
💡 Hint

Subtract one remaining request per call from the initial remaining count.