New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middlewares executed in a wrong order #1850
Comments
@mirfilip order is correct. You should understand the difference between logging directly in middleware and in promise. So, let's see next example: $handler->push(function(callable $handler) use ($logger) {
return function (RequestInterface $request, array $options) use ($handler, $logger) {
// nothing here
return $handler($request, $options)->then(
function ($response) use ($logger) {
$logger->notice('case #1');
return $response;
}
);
};
}, 'first_middleware');
$handler->push(function(callable $handler) use ($logger) {
return function (RequestInterface $request, array $options) use ($handler, $logger) {
$logger->notice('case #2');
return $handler($request, $options)->then(
function ($response) use ($logger) {
// nothing here
return $response;
}
);
};
}, 'second_middleware'); What will you expect to see? Probably So, it's very powerful tool and you can read more about it here: http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html |
@Nyholm could you please close this issue. I think I gave full explanation to author and issue was opened 7 months ago. |
@gregurco First of all, thx for the explanation. I admit, this is quite a hard concept to grasp correctly, as apart from middleware & handlers design, one has to deal with Promises. I now get it that using Now, I get the correct order for request logging. Still, response middlewares (the ones pushed with $stack = HandlerStack::create();
$stack->push(Middleware::mapRequest(function(RequestInterface $request) use ($logger) {
$logger->debug('Adding foo:bar headers');
return $request->withHeader(
'foo',
'bar'
);
}), 'add_headers');
$stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($logger) {
$logger->debug('### Request ###');
return $request;
}), 'log_request_fact');
$stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($logger) {
$message = 'Request: ' . $request->getMethod() . ' ' . (string) $request->getUri() . ' Headers: ' . json_encode($request->getHeaders());
$logger->debug($message);
return $request;
}), 'log_request_details');
$stack->push(Middleware::mapResponse(function(ResponseInterface $response) use ($logger) {
$logger->debug('### Response ###');
return $response;
}), 'log_response_fact');
$stack->push(Middleware::mapResponse(function(ResponseInterface $response) use ($logger) {
$message = 'Response body: ' . (string) $response->getBody();
$logger->debug($message);
return $response;
}), 'log_response_details');
$stack->push(Middleware::mapResponse(function(ResponseInterface $response) use ($logger) {
$newResponse = \GuzzleHttp\json_encode([
'usedId' => 1,
'id' => 1,
'title' => 'This is a test',
], JSON_UNESCAPED_SLASHES);
$newBody = \GuzzleHttp\Psr7\stream_for($newResponse);
$logger->debug('Changed response body to: ' . $newResponse);
return $response->withBody($newBody);
}), 'transform_response_body'); In that case, the order is:
Of course, I can replace |
id also like an answer to this. Why are response middlewares executed in reverse? |
Yes this is by design:
Prints:
but because we have the
|
I'm using version
6.2.3
andfrankkessler/guzzle-oauth2-middleware
for Oauth2 client credentials flow, but that is not important here.EDIT: I've edited the example to be easily reproducible and not have oauth2 related stuff.
Expected middlewares order:
Actual order:
Why is the order not correct?
The text was updated successfully, but these errors were encountered: