CodeAgent
CodeAgent

Reputation: 103

How do i redirect to an external url with headers?

How i have an application which is sitting on A server and i would like to allow user to get to another application which is in B server with a header of the user information. I have done some tries but i m not getting the header in B server. How can i achieve that ya?

Bellow are the codes which i have tried:-

return redirect()->away($apiUrl)->header('x-api-token', $token);

and

$client = new Client();
        $request = $client->request('get', $apiUrl, [
            'headers' => [
                'x-api-user-token' => $userToken
            ]
        ]);

Is there a way for me to redirect to an external url with a header?

Upvotes: 2

Views: 6003

Answers (1)

imrealashu
imrealashu

Reputation: 5099

You might want to try the helper method provided by Laravel and it works like a charm for me.

return redirect('http://external.url/', 302, [
    'custom-header' => 'custom value'
])

If you want to look at the source code please refer /vendor/laravel/framework/src/Illuminate/Foundation/Helpers.php

/**
 * Get an instance of the redirector.
 *
 * @param  string|null  $to
 * @param  int     $status
 * @param  array   $headers
 * @param  bool    $secure
 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
 */
function redirect($to = null, $status = 302, $headers = [], $secure = null)
{
    if (is_null($to)) {
        return app('redirect');
    }

    return app('redirect')->to($to, $status, $headers, $secure);
} 

Upvotes: 5

Related Questions