0
0
Flaskframework~20 mins

HTTP methods (GET, POST, PUT, DELETE) in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTTP Methods Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask route when accessed with a GET request?

Consider this Flask route:

from flask import Flask, request
app = Flask(__name__)

@app.route('/data', methods=['GET', 'POST'])
def data():
    if request.method == 'POST':
        return 'Posted Data'
    else:
        return 'Got Data'

What will the server respond with when a GET request is sent to /data?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/data', methods=['GET', 'POST'])
def data():
    if request.method == 'POST':
        return 'Posted Data'
    else:
        return 'Got Data'
AThe response will be 'Posted Data'
BThe response will be 'Got Data'
CThe server will return a 405 Method Not Allowed error
DThe server will return a 404 Not Found error
Attempts:
2 left
💡 Hint

Think about which method triggers which return statement.

📝 Syntax
intermediate
1:30remaining
Which Flask route definition correctly handles PUT requests?

Choose the correct Flask route decorator to handle PUT requests on the path /update.

A@app.route('/update', methods=['PUT'])
B@app.route('/update', methods='PUT')
C@app.route('/update', method='PUT')
D@app.route('/update', methods=['put'])
Attempts:
2 left
💡 Hint

Check the spelling and type of the methods argument.

🔧 Debug
advanced
2:00remaining
What error does this Flask route raise when accessed with DELETE?

Given this Flask route:

@app.route('/item', methods=['GET', 'POST'])
def item():
    return 'Item endpoint'

What happens if a client sends a DELETE request to /item?

Flask
@app.route('/item', methods=['GET', 'POST'])
def item():
    return 'Item endpoint'
AThe server returns a 404 Not Found error
BThe server raises a TypeError
CThe server returns 'Item endpoint'
DThe server returns a 405 Method Not Allowed error
Attempts:
2 left
💡 Hint

Check which methods are allowed by the route.

state_output
advanced
2:30remaining
What is the final value of the list after these HTTP requests?

Consider this Flask app snippet:

from flask import Flask, request
app = Flask(__name__)
items = []

@app.route('/add', methods=['POST'])
def add():
    items.append(request.json.get('item'))
    return 'Added'

If the client sends these requests in order:

  1. POST to /add with JSON {"item": "apple"}
  2. POST to /add with JSON {"item": "banana"}
  3. GET to /add

What is the value of items after these requests?

Flask
from flask import Flask, request
app = Flask(__name__)
items = []

@app.route('/add', methods=['POST'])
def add():
    items.append(request.json.get('item'))
    return 'Added'
A['apple', 'banana']
B['apple']
C[]
D['banana']
Attempts:
2 left
💡 Hint

Remember that GET requests to this route are not handled.

🧠 Conceptual
expert
1:30remaining
Which HTTP method is idempotent and safe according to REST principles?

In REST API design, which HTTP method is considered both idempotent and safe?

APUT
BPOST
CGET
DDELETE
Attempts:
2 left
💡 Hint

Safe means it does not change server state. Idempotent means multiple identical requests have the same effect as one.