diff --git a/docs/middleware/custom_middleware.md b/docs/middleware/custom_middleware.md index 7cfa8a2..a195a70 100644 --- a/docs/middleware/custom_middleware.md +++ b/docs/middleware/custom_middleware.md @@ -57,7 +57,7 @@ Unlike `process_request`, returning a value here doesn't change anything. We're This is a helper function that is available for you to override; it's not often used by middleware, but there are some ([like the pydantic middleware](middleware/pydantic.md)) that call `on_error` when there is a validation failure. -## post_process(self, request: Request, response: HttpResponse, rendered_response: str) -> str: +## post_process(self, request: Request, response: HttpResponse, rendered_response: str) -> str | bytes: > New in 1.3.0! @@ -67,7 +67,7 @@ There are three things passed to `post_process`: - `request`: the request object. It's provided here purely for reference purposes; while you can technically change it here, it won't have any effect on the response. - `response`: the response object. The full HTML of the response has already been rendered, but the headers can still be modified here. This object can be modified in place, like in `process_response`. -- `rendered_response`: the full HTML of the response as a string. This is the final output that will be sent to the client. Every instance of `post_process` must return the full HTML of the response, so if you want to make changes, you'll need to return the modified string. +- `rendered_response`: the full HTML of the response as a string or bytes. This is the final output that will be sent to the client. Every instance of `post_process` must return the full HTML of the response, so if you want to make changes, you'll need to return the modified string. A string is _strongly_ preferred, but bytes are also acceptable; keep in mind that you'll be making things harder for any `post_process` middleware that comes after you. Note that this function *must* return the full HTML of the response (provided at the start as `rendered_response`. Each invocation of `post_process` overwrites the entire output of the response, so make sure to return everything that you want to send. For example, here's a middleware that ~~breaks~~ adjusts the capitalization of the response and also demonstrates passing variables into the middleware and modifies the headers with the type of transformation: @@ -75,13 +75,13 @@ Note that this function *must* return the full HTML of the response (provided at import random from spiderweb.request import Request +from spiderweb.response import HttpResponse from spiderweb.middleware import SpiderwebMiddleware from spiderweb.exceptions import ConfigError class CaseTransformMiddleware(SpiderwebMiddleware): - # this breaks everything, but it's hilarious so it's worth it. - # Blame Sam. + # this breaks everything, but it's hilarious so it's worth it. Blame Sam. def post_process(self, request: Request, response: HttpResponse, rendered_response: str) -> str: valid_options = ["spongebob", "random"] # grab the value from the extra data passed into the server object @@ -109,6 +109,7 @@ class CaseTransformMiddleware(SpiderwebMiddleware): ) # usage: +from spiderweb import SpiderwebRouter app = SpiderwebRouter( middleware=["CaseTransformMiddleware"],