Skip to content
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

HTTP Client beforeSending does not take callables #41486

Closed
MariusSpring opened this issue Mar 15, 2022 · 1 comment
Closed

HTTP Client beforeSending does not take callables #41486

MariusSpring opened this issue Mar 15, 2022 · 1 comment

Comments

@MariusSpring
Copy link

MariusSpring commented Mar 15, 2022

  • Laravel Version: 8.7
  • PHP Version: 8.1
  • Database Driver & Version: 5.7

Description:

The beforeSending method on the PendingRequest class says that it takes a callable $callback, however, this is not entirely true. When the callback is called, it is done so using $callback->__invoke(...) and not $callback(...). This makes the following syntax result in error "Call to a member function __invoke() on array":

protected function buildRequest(): PendingRequest
{
    return Http::baseUrl('https://api.example.org')
        ->beforeSending([ $this, 'prepareRequest' ])
        ->asJson()
        ->withoutVerifying();
}

protected function prepareRequest(Request $request, array $options, PendingRequest $pendingRequest)
{
    $headers = [
        'X-Test-A' => 'test',
        'X-Test-B' => 'test',
        'X-Test-C' => 'test',
    ];

    $pendingRequest->withHeaders($headers);
}

I expect the code above to execute the prepareRequest method as the callable with the params passed. This happens if the callback would be called using $callback(...).

Steps To Reproduce:

  1. Create a PendingRequest using Http fascade.
  2. Set beforeSending to a callable referencing method on object using array syntax as demonstrated in description example,
  3. Send request.
  4. See error.
@driesvints
Copy link
Member

Thanks, I've sent in a PR for this: #41489

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants