Complete the code to define a Flask route that responds to GET requests.
from flask import Flask app = Flask(__name__) @app.route('/hello', methods=[[1]]) def hello(): return 'Hello, world!'
The methods parameter specifies which HTTP methods the route accepts. To respond to GET requests, use "GET".
Complete the code to handle POST requests in a Flask route.
from flask import Flask, request app = Flask(__name__) @app.route('/submit', methods=[[1]]) def submit(): data = request.form.get('data') return f'Received: {data}'
POST requests are used to send data to the server, so the route must accept "POST" to handle form data.
Fix the error in the Flask route to allow updating data with PUT method.
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}'
The PUT method is used to update existing data, so the route must accept "PUT" to handle update requests.
Fill in the blank to create a Flask route that deletes a resource using DELETE method and returns a confirmation message.
from flask import Flask app = Flask(__name__) @app.route('/delete-item', methods=[[1]]) def delete_item(): # code to delete item return 'Item deleted!'
The DELETE method is used to remove resources, so the route must accept "DELETE". The return message can be any string; here, we use a simple confirmation message.
Fill all three blanks to create a Flask route that supports GET and POST methods and returns different messages based on the request method.
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'
The route must accept both GET and POST methods. The request.method is compared to "GET" to decide the response.