Reputation: 46178
I'm using the HTTPClient with Symfony 5.2
$response = $myApi->request($method, $url, $options);
When a request fails, I'd like to get the detailed info about the original request, ie: the request headers.
$response->getInfo()
does not return them (only the response headers).
My $options
don't always have all that I need because some can come from the client config.
I need to log that somewhere in production, I saw maintainers working on injecting a logger but didn't find more info about it.
After a quick check on the code, I can see that a logger can be set but it seems to log only the method and URI.
How can I get the request info like headers or params/body ?
Github Issue opened about this
Upvotes: 6
Views: 7868
Reputation: 46178
$response->getInfo('debug')
contains the request headers once they have been received by the client.
dump($response->getInfo('debug'));
* Found bundle for host myapi: 0x7fe4c3f81580 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host myapi
* Connected to myapi (xxx.xxx.xxx.xxx) port xxxx (#0)
> POST /my/uri/ HTTP/1.1
Host: myapi:xxxx
Content-Type: application/json
Accept: */*
Authorization: Bearer e5vn9569-n76v9nd-v6n978-dv6n98
User-Agent: Symfony HttpClient/Curl
Accept-Encoding: gzip
Content-Length: 202
* upload completely sent off: 202 out of 202 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 8
< ETag: W/"8-M1u4Sc28uxk+zyXJvSTJaEkyIGw"
< Date: Tue, 06 Apr 2021 07:36:10 GMT
< Connection: keep-alive
<
* Connection #0 to host myapi left intact
Also TraceableHttpClient seems to be designed for detailed debuging
Upvotes: 5
Reputation: 55427
Here's a quick sample of one of the options that I posted in my comment:
public function __construct(HttpClientInterface $client, LoggerInterface $logger)
{
$this->client = $client;
if ($this->client instanceof LoggerAwareInterface) {
$this->client->setLogger($logger);
}
}
You could also delay the setLogger
to your use area, too.
Upvotes: 2