Flask Response Response Object API
The Response class represents the HTTP response returned by a Flask view function. In most cases you do not need to create it directly—Flask automatically converts the view function's return value into a Response.
When manual control is needed, use the make_response() function.
constructor
| Parameters | Type | Default value | Description |
|---|---|---|---|
| response | str / bytes / iterator | Required | Response body content |
| status | int / str | 200 | HTTP status code or status text, such as 404 or "404 Not Found" |
| headers | dict / list | None | Response headers, a dict or a list of (key, value) tuples |
| mimetype | str | None | MIME type, such as "application/json" |
| content_type | str | None | Content-Type header, including encoding, such as "text/html; charset=utf-8" |
| direct_passthrough | bool | False | When True, pass the response body directly without any processing |
Core Attributes
| property | Type | Description |
|---|---|---|
| status | str | Response status (text form), such as "200 OK", "404 Not Found" |
| status_code | int | HTTP status code, such as 200, 404 |
| headers | Headers | Response headers object, supporting dictionary-style operations |
| data | bytes | Byte representation of the response body |
| mimetype | str | MIME type (excluding encoding), such as "text/html" |
| content_type | str | Content-Type (including encoding), such as "text/html; charset=utf-8" |
| content_length | int | Byte length of the response body |
Response header operations
| Method | Description |
|---|---|
| headers.get(key) | Get the value of a specified response header |
| headers[key] = value | Set a response header |
| headers.update(dict) | Update response headers in bulk |
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 a specified cookie |
| set_cookie parameter: max_age | Number of seconds the cookie is valid, e.g. max_age=3600 means 1 hour |
| set_cookie parameter: secure | Defaults to the SESSION_COOKIE_SECURE config value; when True, sent only over HTTPS |
| set_cookie parameter: httponly | Defaults to True, preventing JavaScript from accessing this cookie |
| set_cookie parameter: samesite | "Strict", "Lax", or None, controlling cross-site cookie sending behavior |
Other methods
| Method | Description |
|---|---|
| get_json(force=False, silent=False) | Parse the response body as JSON (mainly used in testing) |
| freeze() | Freeze the response into an immutable state |
| force_type(response, environ) | Class method that forcibly converts other types of responses into the Response type |
Code Example
Example
from
flask
import
Flask
,
make_response
app
=
Flask
(
name
)
@
app.
route
(
"/custom"
)
def
custom_response
(
)
:
Manually building a response with make_response
resp
=
make_response
(
"
Hello, RUNOOB!
")
Set the status code
resp.
status_code
=
201
Set custom response headers
resp.
headers
[
"X-Custom-Header"
]
=
"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 deleted"
)
resp.
delete_cookie
(
"theme"
)
return
resp
@
app.
route
(
"/api/data"
)
def
api_data
(
)
:
Return the dict directly, and Flask automatically serializes it into a JSON Response
return
{
"status"
:
"ok"
,
"data"
:
[
1
,
2
,
3
]
}