Represents an outgoing WSGI HTTP response with body, status, and headers. Has properties and methods for using the functionality defined by various HTTP specs. The response body is flexible to support different use cases. The simple form is passing bytes, or a string which will be e
| 37 | |
| 38 | |
| 39 | class Response(_SansIOResponse): |
| 40 | """Represents an outgoing WSGI HTTP response with body, status, and |
| 41 | headers. Has properties and methods for using the functionality |
| 42 | defined by various HTTP specs. |
| 43 | |
| 44 | The response body is flexible to support different use cases. The |
| 45 | simple form is passing bytes, or a string which will be encoded as |
| 46 | UTF-8. Passing an iterable of bytes or strings makes this a |
| 47 | streaming response. A generator is particularly useful for building |
| 48 | a CSV file in memory or using SSE (Server Sent Events). A file-like |
| 49 | object is also iterable, although the |
| 50 | :func:`~werkzeug.utils.send_file` helper should be used in that |
| 51 | case. |
| 52 | |
| 53 | The response object is itself a WSGI application callable. When |
| 54 | called (:meth:`__call__`) with ``environ`` and ``start_response``, |
| 55 | it will pass its status and headers to ``start_response`` then |
| 56 | return its body as an iterable. |
| 57 | |
| 58 | .. code-block:: python |
| 59 | |
| 60 | from werkzeug.wrappers.response import Response |
| 61 | |
| 62 | def index(): |
| 63 | return Response("Hello, World!") |
| 64 | |
| 65 | def application(environ, start_response): |
| 66 | path = environ.get("PATH_INFO") or "/" |
| 67 | |
| 68 | if path == "/": |
| 69 | response = index() |
| 70 | else: |
| 71 | response = Response("Not Found", status=404) |
| 72 | |
| 73 | return response(environ, start_response) |
| 74 | |
| 75 | :param response: The data for the body of the response. A string or |
| 76 | bytes, or tuple or list of strings or bytes, for a fixed-length |
| 77 | response, or any other iterable of strings or bytes for a |
| 78 | streaming response. Defaults to an empty body. |
| 79 | :param status: The status code for the response. Either an int, in |
| 80 | which case the default status message is added, or a string in |
| 81 | the form ``{code} {message}``, like ``404 Not Found``. Defaults |
| 82 | to 200. |
| 83 | :param headers: A :class:`~werkzeug.datastructures.Headers` object, |
| 84 | or a list of ``(key, value)`` tuples that will be converted to a |
| 85 | ``Headers`` object. |
| 86 | :param mimetype: The mime type (content type without charset or |
| 87 | other parameters) of the response. If the value starts with |
| 88 | ``text/`` (or matches some other special cases), the charset |
| 89 | will be added to create the ``content_type``. |
| 90 | :param content_type: The full content type of the response. |
| 91 | Overrides building the value from ``mimetype``. |
| 92 | :param direct_passthrough: Pass the response body directly through |
| 93 | as the WSGI iterable. This can be used when the body is a binary |
| 94 | file or other iterator of bytes, to skip some unnecessary |
| 95 | checks. Use :func:`~werkzeug.utils.send_file` instead of setting |
| 96 | this manually. |
no outgoing calls