Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rankus's avatar

Illuminate\Http\Client\PendingRequest : set headers in beforeSending callback

Hello, I'm trying to add headers to outgoing PendingRequests from within beforeSending callback. I guess it's the right moment to be sure everything I need is set to populate my headers' data.

Here is my callback:

public function buildRequestToHeaders(\Illuminate\Http\Client\Request $currentRequest, array $options, PendingRequest $requestTo): PendingRequest
{
    $headers['x-forwarded-for'] = request()->ip();
    $headers['x-my-custom-header'] = 'custom data computed from within the app';
    $requestTo->withHeaders($headers);
    
    return $requestTo;
}

The problem is that my headers go into $options but not into the embedded Guzzle client.

Do you know if it's possible ? How can I rebuild the Guzzle client so that it takes the new headers ?

0 likes
2 replies
johnybanana's avatar

Hi, did you ever figure this out? I want to add some data to the body for every request, but facing the same issues

Rankus's avatar

Hi, I simply moved outside of beforeSending.

1 like

Please or to participate in this conversation.