graze / guzzle-jsonrpc

JSON-RPC 2.0 client for Guzzle
https://packagist.org/packages/graze/guzzle-jsonrpc
MIT License
93 stars 61 forks source link

Possibility to set headers #27

Closed Halfi closed 8 years ago

Halfi commented 8 years ago

At now haven't way to set Guzzle headers. I can't send request with basic auth.

sjparkinson commented 8 years ago

I believe you should be able to achieve this using a handler. You can find more info on them in the guzzle docs.

It might look something like:

function add_header($header, $value)
{
    return function (callable $handler) use ($header, $value) {
        return function (
            RequestInterface $request,
            array $options
        ) use ($handler, $header, $value) {
            $request = $request->withHeader($header, $value);
            return $handler($request, $options);
        };
    };
}

$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(add_header('Authorization', 'Basic QWxhZGRpbjpPcGVuU2VzYW1l'));

$client = Client::factory('http://localhost:8000', ['handler' => $stack]);
Halfi commented 8 years ago

Thanks, but I'm find more simple way without handler callback:

raze\GuzzleHttp\JsonRpc\Client::factory(
    'http://localhost:8000',
    [
        'auth' => ['user', 'secret']
    ]
);

It's work with Guzzle 6. If guzzle 5 there are some different way:

\Graze\GuzzleHttp\JsonRpc\Client::factory(
    'http://localhost:8000',
    [
        'defaults' => [
            'auth' => ['user', 'secret']
        ]
    ]
);
sjparkinson commented 8 years ago

Even better! Thanks for sharing your solution.