From 956ed013cf98825737ff20df78d6ecb7aa378c9a Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 12 Nov 2021 12:08:13 +0200 Subject: [PATCH] Improved redirects: send redirect response in JSON if the request was in JSON --- CHANGELOG.md | 1 + system/src/Grav/Common/Grav.php | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb744b44d6..5ea47df75f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Improved page cloning, added method `Page::initialize()` * Improved `FlexObject::getChanges()`: return changed lists and arrays as whole instead of just changed keys/values * Improved form validation JSON responses to contain list of failed fields with their error messages + * Improved redirects: send redirect response in JSON if the request was in JSON 3. [](#bugfix) * Fixed path traversal vulnerability when using `bin/grav server` * Fixed unescaped error messages in JSON error responses diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 1169b74f67..2398c116c0 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -464,6 +464,10 @@ public function getRedirectResponse($route, $code = null): ResponseInterface } } + if ($uri->extension() === 'json') { + return new Response(200, ['Content-Type' => 'application/json'], json_encode(['code' => $code, 'redirect' => $url], JSON_THROW_ON_ERROR)); + } + return new Response($code, ['Location' => $url]); }