Replies: 3 comments 1 reply
-
True. What can be override is NOT the response content but the response object itself. Depending on the response type, the response content can be a string, binary, callback, json object etc, all handled by different response implementations. What you are seeking for it much more templating than middleware. |
Beta Was this translation helpful? Give feedback.
-
Hmm, well... The code I provided was just an example. Maybe providing the actual code I'm working on will explain it better. This is what I've managed to come up with over the past few days: class PrettyResponses : public AsyncMiddleware
{
public:
void run(AsyncWebServerRequest *request, ArMiddlewareNext next) override {
next(); // continue middleware chain
AsyncWebServerResponse *response = request->getResponse();
int code = response->code();
if (code == 200) return; // shortcut
switch (code)
{
case 401: // User hasn't logged in, missing credentials
response = request->beginResponse(
401, "text/plain", "401: Unauthorized"
);
response->addHeader("Refresh", "4; url=/login");
request->send(response);
break;
case 403: // Credentials are present, but they are not valid
response = request->beginResponse(
403, "text/plain", "403: Forbidden"
);
response->addHeader("Refresh", "4; url=/login");
request->send(response);
break;
case 400:
case 404:
case 500:
case 503:
String content = String(code);
content.concat(": ");
content.concat(response->responseCodeToString(code));
request->send(code, "text/plain", content);
break;
}
}
}; The purpose of this is to act as a "postprocessor" for all responses being sent back from the server. Before, every time I needed to return a 401 or 403, I'd need to state the content type and content to it. Now, I can just What I've ran into, was me trying to provide custom messages in the responses, in addition to stating just the response code. So the plan was to use for ex. So, instead of this, I'd need to remove the middleware, and use a function that'd template in those messages (or use the default message)? |
Beta Was this translation helpful? Give feedback.
-
I see. Alright, I'll go with that. Thank you for your help =) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm trying to make a middleware that would modify the current response's content. I know I can replace the current response in a request by just creating a new response and sending that instead, but to modify a response, I need to know the current content first, and it seems that there's currently no way to obtain that. I've searched through the documentation and the code, and it seems that
AsyncWebServerResponse
stores it's response content under a_content
private attribute, so there's no way to read this, at least currently.Something like this would be cool to have:
On a similar note, there's a way to set content type, but not get content type. The response code has both, thankfully. And the headers seem to have their respective methods too:
Beta Was this translation helpful? Give feedback.
All reactions