Skip to content

HttpMethodsClient not working with PSR-17 #201

Closed

Description

@jdecool

Description
The HttpMethodsClient constructor allows to get an RequestFactory or RequestFactoryInterface as second parameter.

Both declare the createRequest but don't have the same argument.

RequestFactoryInterface::createRequest only have two parameters method and uri.

The HttpMethodsClient::send method which is used to make the HTTP call have two other parameters. So when using a PSR-17 implementation, the code will not work as the body and/or headers won't be sent.

How to reproduce

$http = new HttpMethodsClient(HttpClientDiscovery::find(), Psr17FactoryDiscovery::findRequestFactory());
$response = $http->get($uri, $headers);

Headers will not be sent.

Activity

  1. GrahamCampbell commented on Jul 18, 2020

    @GrahamCampbell
    Contributor

    I cannot replicate this. Which particular client and request factory are you discovering. Maybe they have a bug?

    EDIT: I couldn't replicate this because my request factory happened to implement both interfaces.

  2. jdecool commented on Jul 18, 2020

    @jdecool
    Author

    I use guzzle6-adapter as it doesn't provide a PSR-17 implementation, I use it with nyholm/psr7.

    But beyond that, if we look at the interfaces definitions:

    namespace Http\Message;
    
    interface RequestFactory
    {
        public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1');
    }
    namespace Psr\Http\Message;
    
    interface RequestFactoryInterface
    {
        public function createRequest(string $method, $uri): RequestInterface;
    }

    And how the send method is implemented:

    public function send(string $method, $uri, array $headers = [], $body = null): ResponseInterface
    {
        return $this->sendRequest($this->requestFactory->createRequest(
            $method,
            $uri,
            $headers,
            $body
        ));
    }

    There is a problem.

    Do you have an example with a working PSR-17 implementation ?

  3. GrahamCampbell commented on Jul 18, 2020

    @GrahamCampbell
    Contributor

    php-http/guzzle6-adapter:^2.0.1 http-interop/http-factory-guzzle:^1.0 should work correctly. You don't need two different psr7 implementations (Guzzle already provides one).

    NB You can also use Guzzle 7: guzzlehttp/guzzle:^7.0.1 http-interop/http-factory-guzzle:^1.0.

  4. jdecool commented on Jul 18, 2020

    @jdecool
    Author

    Not working.

    I use those dependencies:

    "require": {
            "php": "^7.3",
            "ext-json": "*",
            "php-http/client-common": "^2.2",
            "php-http/client-implementation": "^1.0",
            "php-http/discovery": "^1.6",
            "php-http/httplug": "^2.0",
            "http-interop/http-factory-guzzle": "^1.0",
            "php-http/guzzle6-adapter": "^2.0"
    },

    With this code:

    $http = new Http\Client\Common\HttpMethodsClient(
        Http\Discovery\HttpClientDiscovery::find(),
        Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(),
    );
    
    $response = $http->get('http://foo/bar', ['My-Header' => 'Value']);

    A debug in HttpMethodsClient::send:

    image

    And in it's implementation:

    image

    Headers are not set.

  5. jdecool commented on Jul 18, 2020

    @jdecool
    Author

    I don't understand how the code could work as the two interfaces allowed in the HttpMethodsClient don't have the same contract.

  6. GrahamCampbell commented on Jul 18, 2020

    @GrahamCampbell
    Contributor

    Thanks for those details. Very helpful. :)

        "php-http/client-implementation": "^1.0",
    

    You don't need this line, in fact you should not add it.

    Headers are not set.

    Let me take a look at this in more detail...

  7. GrahamCampbell commented on Jul 18, 2020

    @GrahamCampbell
    Contributor

    I think you absolutely are right, and the reason I hadn't seen this bug before is because I happened to be using something that was also a PHP-HTTP request factory, and not just a PSR one. There is definitely a bug here. I will prepare a PR to fix this. :)

  8. GrahamCampbell commented on Jul 18, 2020

    @GrahamCampbell
    Contributor

    @jdecool Please let me know what you think of #202.

  9. jdecool commented on Jul 18, 2020

    @jdecool
    Author

    Please let me know what you think of #202.

    Look good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions