request.args contain in Flask?Consider a Flask route that handles a URL like /search?query=flask&sort=asc. What does request.args represent?
from flask import Flask, request app = Flask(__name__) @app.route('/search') def search(): return str(request.args)
Think about what appears after the question mark in a URL.
request.args holds the query parameters from the URL as a dictionary-like object. It does not contain form data or JSON body.
request.method for a POST request?Given a Flask route that accepts both GET and POST, what will request.method return when the client sends a POST request?
from flask import Flask, request app = Flask(__name__) @app.route('/submit', methods=['GET', 'POST']) def submit(): return request.method
HTTP methods are uppercase strings in Flask's request.method.
request.method returns the HTTP method used for the request as an uppercase string, so for POST requests it returns 'POST'.
Assuming a client sends JSON data {"name": "Alice"} in a POST request, which code correctly extracts the value of name?
from flask import Flask, request app = Flask(__name__) @app.route('/json', methods=['POST']) def json_route(): # Extract name from JSON here pass
Use the method that parses JSON safely from the request body.
request.get_json() parses the JSON body and returns a dictionary. request.json is a property but can be None if content type is wrong. request.data is raw bytes, and request.form is for form data.
request.form['username'] raise a KeyError?A Flask route expects form data with a field username. The code request.form['username'] raises a KeyError. What is the most likely reason?
from flask import Flask, request app = Flask(__name__) @app.route('/login', methods=['POST']) def login(): user = request.form['username'] return f"Hello {user}"
Think about what happens if the expected form field is missing.
If the client does not send a form field named 'username', accessing it with request.form['username'] raises a KeyError. Using request.form.get('username') avoids this error.
request property provides the raw bytes of the request body?In Flask, you want to access the raw bytes sent in the request body regardless of content type. Which request property gives you this?
Think about the raw content before parsing.
request.data contains the raw bytes of the request body. request.form is parsed form data, request.json is parsed JSON, and request.args is query parameters.