Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set headers for forwarded request

My controller's methods require a header to be set, e.g. X-Authorization. After a new object has been created (store action), I do a forward to show the newly created object (show action):

$request = Request::create(route('api.v1.b.show', ['booking' => 4]), 'GET');
Request::replace($request->input());
return Route::dispatch($request);

The forwarding works ok if I disable the authorization check, but it fails otherwise. ie. the header has gone. I would like to copy the request header, which I can get with Request::header('X-Authorization') into the forwarded request. Is it possible?

I have tried without success to do $request->header('X-Authorization', 'xxxxx'). Also tried PHP's header() before the dispatch and didn't work.

Any ideas? Cheers

like image 927
clapas Avatar asked Sep 30 '15 15:09

clapas


People also ask

What are forwarded headers?

The Forwarded request header contains information that may be added by reverse proxy servers (load balancers, CDNs, and so on) that would otherwise be altered or lost when proxy servers are involved in the path of the request.

How do I send a header in HTTP request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers.

How do I create a custom header in request?

Under Custom response headers, click Add header. Enter the Header name and Header value for the custom response header. Enter any additional custom response headers. Click Save.


1 Answers

Ok, i think you need to set the headers like so:

$request = Request::create(route('api.v1.b.show', ['booking' => 4]), 'GET');
$request->headers->set('X-Authorization', 'xxxxx');

That is the answer to your question.

My question is: Where can we set this headers for every api request(forwarding)? Because i personally have 5 headers to set with the request and i don't want to repeat myself.

like image 82
musicvicious Avatar answered Oct 03 '22 18:10

musicvicious