Skip to content

Changing status code of response with middleware #406

Closed Answered by gregrobson
gregrobson asked this question in Q&A
Discussion options

You must be logged in to vote

Five minutes later I found a test in the source code and was able to see the pattern of how to reincorporate a new response. I'm sure I had looked before! 😆

Answer if anyone has the same issue!

    public function __invoke(Response $response): Response
    {
        if (empty($response->body['users'])) {
            $psrResponse = $response->getPsrResponse();
            $newPsrResponse = $psrResponse->withStatus(404);
            return $response::fromPsrResponse($newPsrResponse, $response->getPendingRequest(), $response->getPsrRequest());
        }

        return $response;
    }

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Sammyjo20
Comment options

Answer selected by Sammyjo20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants