From cf9ae09db8db47eb09c95dde99794ebce94dc0ba Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Mon, 30 Sep 2024 12:07:45 +0900 Subject: [PATCH] add optional responce code to AsyncWebServerRequest::redirect() method Allows to specify differents redirect codes to use, i.e. 301/302, 307/308 --- src/ESPAsyncWebServer.h | 9 +++++---- src/WebRequest.cpp | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 2857884..ebe0edf 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -288,12 +288,13 @@ class AsyncWebServerRequest { } /** - * @brief issue 302 redirect response + * @brief issue HTTP redirect responce with Location header * - * @param url + * @param url - url to redirect to + * @param code - responce code, default is 302 : temporary redirect */ - void redirect(const char* url); - void redirect(const String& url) { return redirect(url.c_str()); }; + void redirect(const char* url, int code = 302); + void redirect(const String& url, int code = 302) { return redirect(url.c_str(), code); }; void send(AsyncWebServerResponse* response); AsyncWebServerResponse* getResponse() const { return _response; } diff --git a/src/WebRequest.cpp b/src/WebRequest.cpp index daa9e07..1ef83e5 100644 --- a/src/WebRequest.cpp +++ b/src/WebRequest.cpp @@ -766,8 +766,8 @@ void AsyncWebServerRequest::send(AsyncWebServerResponse* response) { send(500); } -void AsyncWebServerRequest::redirect(const char* url) { - AsyncWebServerResponse* response = beginResponse(302); +void AsyncWebServerRequest::redirect(const char* url, int code) { + AsyncWebServerResponse* response = beginResponse(code); response->addHeader(T_LOCATION, url); send(response); }