setono/request-aware-http-client

A library that decorates the http client with request logging

v1.1.0 2023-05-19 11:34 UTC

This package is auto-updated.

Last update: 2024-02-07 12:21:43 UTC


README

Latest Version Software License Build Status Code Coverage

This library will decorate Symfony HTTP client and make it possible to retrieve full requests for debugging etc.

Installation

Step 1: Download

composer require setono/request-aware-http-client

Usage

<?php
use Setono\RequestAwareHttpClient\RequestAwareHttpClient;
use Setono\RequestAwareHttpClient\RequestAwareHttpClientInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class YourService
{
    private RequestAwareHttpClientInterface $httpClient;
    
    public function __construct(HttpClientInterface $httpClient)
    {
        $this->httpClient = new RequestAwareHttpClient($httpClient);
    }
    
    public function doSomething(): void
    {
        $response = $this->httpClient->request('POST', 'https://httpbin.org/post', [
            'json' => ['name' => 'John Doe']
        ]);

        $request = $this->httpClient->getRequest($response);

        echo $request->toString();
        
        // Outputs:
        // POST https://httpbin.org/post
        // {
        //     "name": "John Doe"
        // }
    }
}