YouTip LogoYouTip

Flask Response Api

The Response class represents the HTTP response returned by Flask view functions. In most cases, you don't need to create it directly β€” Flask automatically converts the return value of the view function to a Response. When manual control is needed, use the make_response() function. * * * ## Constructor | Parameter | Type | Default | Description | | --- | --- | --- | --- | | response | str / bytes / iterator | Required | Response body content | | status | int / str | 200 | HTTP status code or status text, e.g., 404 or "404 Not Found" | | headers | dict / list | None | Response headers, dict or (key, value) tuple list | | mimetype | str | None | MIME type, e.g., "application/json" | | content_type | str | None | Content-Type header, including encoding, e.g., "text/html; charset=utf-8" | | direct_passthrough | bool | False | When True, directly passes the response body without any processing | * * * ## Core Properties | Property | Type | Description | | --- | --- | --- | | status | str | Response status (in text form), e.g., "200 OK", "404 Not Found" | | status_code | int | HTTP status code, e.g., 200, 404 | | headers | Headers | Response headers object, supports dictionary-style operations | | data | bytes | Byte representation of the response body | | mimetype | str | MIME type (without encoding), e.g., "text/html" | | content_type | str | Content-Type (with encoding), e.g., "text/html; charset=utf-8" | | content_length | int | Response body length in bytes | * * * ## Response Header Operations | Method | Description | | --- | --- | | headers.get(key) | Get the value of the specified response header | | headers = value | Set a response header | | headers.update(dict) | Batch update response headers | * * * ## Cookie Operations | Method | Description | | --- | --- | | set_cookie(key, value, max_age, expires, path, domain, secure, httponly, samesite) | Set a Cookie. max_age is in seconds, expires is a datetime, secure is HTTPS only | | delete_cookie(key, path, domain) | Delete the specified Cookie | | set_cookie parameter: max_age | Cookie validity in seconds, e.g., max_age=3600 means 1 hour | | set_cookie parameter: secure | Defaults to SESSION_COOKIE_SECURE config value, when True only sent over HTTPS | | set_cookie parameter: httponly | Defaults to True, prevents JavaScript access to this Cookie | | set_cookie parameter: samesite | "Strict", "Lax", or None, controls cross-site Cookie sending behavior | * * * ## Other Methods | Method | Description | | --- | --- | | get_json(force=False, silent=False) | Parse the response body as JSON (mainly used for testing) | | freeze() | Freeze the response to an immutable state | | force_type(response, environ) | Class method, forces conversion of other response types to Response type | * * * ## Code Examples ## Example from flask import Flask, make_response app = Flask( __name__ ) @app.route("/custom") def custom_response(): # Use make_response to manually build a response resp = make_response("

Hello, TUTORIAL!

") # Set status code resp.status_code=201 # Set custom response header resp.headers="my-value" # Set Cookie resp.set_cookie("theme","dark", max_age=86400)# Valid for 24 hours resp.set_cookie("lang","zh-CN", samesite="Lax")# SameSite restriction return resp @app.route("/delete-cookie") def delete_cookie(): resp = make_response("Cookie has been deleted") resp.delete_cookie("theme") return resp @app.route("/api/data") def api_data(): # Directly return dict, Flask automatically serializes to JSON Response return{"status": "ok","data": [1,2,3]}
← Flask Session Interface ApiFlask Blueprint Api β†’