0
0
Flaskframework~10 mins

HTTP methods (GET, POST, PUT, DELETE) in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Flask route that responds to GET requests.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/hello', methods=[[1]])
def hello():
    return 'Hello, world!'
Drag options to blanks, or click blank then click option'
A"PUT"
B"POST"
C"GET"
D"DELETE"
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for a route meant to retrieve data.
Forgetting to put the method name in quotes.
2fill in blank
medium

Complete the code to handle POST requests in a Flask route.

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

@app.route('/submit', methods=[[1]])
def submit():
    data = request.form.get('data')
    return f'Received: {data}'
Drag options to blanks, or click blank then click option'
A"GET"
B"POST"
C"PUT"
D"DELETE"
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST when expecting form data.
Not specifying the POST method in the route.
3fill in blank
hard

Fix the error in the Flask route to allow updating data with PUT method.

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

@app.route('/update', methods=[[1]])
def update():
    new_data = request.json.get('new_data')
    return f'Updated to: {new_data}'
Drag options to blanks, or click blank then click option'
A"PUT"
B"DELETE"
C"GET"
D"POST"
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or GET instead of PUT for update operations.
Not including the PUT method in the route's methods list.
4fill in blank
hard

Fill in the blank to create a Flask route that deletes a resource using DELETE method and returns a confirmation message.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/delete-item', methods=[[1]])
def delete_item():
    # code to delete item
    return 'Item deleted!'
Drag options to blanks, or click blank then click option'
A"DELETE"
B"POST"
C"GET"
D"PUT"
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET or POST instead of DELETE for deletion.
Returning no confirmation message.
5fill in blank
hard

Fill all three blanks to create a Flask route that supports GET and POST methods and returns different messages based on the request method.

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

@app.route('/data', methods=[[1], [2]])
def data():
    if request.method == [3]:
        return 'This is a GET request'
    else:
        return 'This is a POST request'
Drag options to blanks, or click blank then click option'
A"GET"
B"POST"
D"PUT"
Attempts:
3 left
💡 Hint
Common Mistakes
Not including both GET and POST in the methods list.
Comparing request.method to the wrong string.