The Request and Response Objects
The request is the 5: the GET (the query, the .get, the no the 1), the POST (the form, the no the 1), the json (the API, the no the the), the files (the upload, the no the 1), and the headers (the no the 1, the no the the). The response is the 4: the string (the 1, the no the 1), the tuple (the 3, the status, the no the 1), the Response (the object, the header, the no the 1), and the redirect (the 302, the no the 1). This chapter covers the request (the 5, the no the 1), the response (the 4, the no the 1), the JSON (the jsonify, the no the 1), and the file (the upload, the no the 1).
The request: The 5 read
# the REQUEST (the 5, the read,
# the no write, the no the 1):
from flask import request
@app.route("/post/",
methods=["GET", "POST"])
def post():
# the 1: the GET (the
# query, the .get,
# the no the 1):
page = request.args.get(
"page", 1, type=int)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the 2: the POST (the
# form, the no the 1):
body = request.form.get(
"body", "").strip()
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the 3: the JSON (the
# API, the no the the):
data = request.get_json(
silent=True)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the 4: the FILES (the
# upload, the no the 1):
f = request.files.get("photo")
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the 5: the HEADERS (the
# no the 1, the no
# the the):
agent = request.headers.get(
"User-Agent", "")
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
return "the ok"
The request is the 5: the args is the GET (the .get the default, the type=int the cast, the no the 1), the form is the POST (the .get, the no the 1, the no the the), the get_json(silent=True) is the API (the no the the, the 400 the no, the 1 the the), the files is the upload (the filename, the save, the no the 1), and the headers is the no (the User-Agent, the no the 1, the no the the) - the no is the write (the immutable, the no the 1, the no the the) - the response is the next: the 4 the no the 1.
The response, the JSON, and the file
# the RESPONSE (the 4, the no
# the 1, the no the the):
from flask import Response, \
jsonify, redirect, url_for
# the 1: the string (the 1,
# the no the 1):
@app.route("/")
def index():
return "the 1"
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the 2: the tuple (the 3,
# the status, the no
# the 1):
@app.route("/the-1/")
def the_1():
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
return "the ok", 201
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the 3: the Response (the
# object, the header,
# the no the 1):
@app.route("/the-2/")
def the_2():
resp = Response("the ok")
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
resp.status_code = 201
resp.headers["X-Custom"] = "1"
return resp
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the 4: the JSON (the API,
# the jsonify, the no
# the 1):
@app.route("/api/post/<int:id>")
def api_post(id):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
return jsonify(id=id,
ok=True), 200
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The response is the 4: the string is the 1 (the no the 1, the no the the), the tuple is the 3 (the status, the 201, the no the 1), the Response is the object (the status_code, the headers, the no the 1), and the jsonify is the API (the dict, the no the 1, the no the the) - the redirect is the 302 (the url_for, the no the 1, the no the the). The file is the upload: the request.files is the 1 (the filename, the no the 1), the secure_filename is the no (the ../, the no the the), and the save is the 1 (the UPLOAD, the no the 1, the no the the) - the rule is the one: the request the 5, the response the 4, the JSON the jsonify, and the file the upload - the next chapter's template is the Jinja2 the render the block the no the 1.
The Working Rules
- The request is the 5, the read, the no write. The args the GET (the .get, the type=int, the no the 1), and the form the POST (the .get, the no the 1) - the get_json the API (the silent, the no the the, the 400 the no), the files the upload (the filename, the save), and the headers the no (the User-Agent, the no the 1).
- The response is the 4, the status the 201. The string the 1 (the no the 1), and the tuple the 3 (the status, the 201, the no the 1) - the Response the object (the status_code, the headers, the no the 1), and the jsonify the API (the dict, the no the 1, the no the the).
- The redirect is the 302, the PRG the no. The url_for the no (the 1, the no the the), and the no the 1 (the no the 1, the no the the) - the no the 301 (the 302, the no the 1), and the no the the (the no the 1, the no the the).
- The file is the upload, the secure_filename the no. The request.files the 1 (the filename, the no the 1), and the secure_filename the no (the ../, the no the the) - the save the 1 (the UPLOAD, the no the 1), and the no the 1 (the no the 1, the no the the).
Summary
The request is the 5: the args the GET (the .get, the type=int, the no the 1), the form the POST (the .get, the no the 1), the get_json the API (the silent, the no the the, the 400 the no), the files the upload (the filename, the save), and the headers the no (the User-Agent, the no the 1). The response is the 4: the string the 1, the tuple the 3 (the status, the 201), the Response the object (the status_code, the headers), and the jsonify the API (the dict, the no the 1). The redirect is the 302 (the url_for, the no the 1), and the file is the upload (the secure_filename, the no the 1, the save the UPLOAD). The rule is the one: the request the 5, the response the 4, the JSON the jsonify, and the file the upload - the next chapter's template is the Jinja2: the render_template the 1, the block the no, and the filter the 20 (the no the 1, the no the the).