3

I am actually completely baffled that this is such a difficult task to accomplish and/or find any relevant information about. My guess is that it must be something SO simple, that no one has to ask about it (except for me! :-) ), so I am hoping that someone can easily point me in the right direction...

I need to set headers in my Requests - not in my Responses (I've got that part handled), and not for Ajax routes (I've got that part handled as well). How on Earth do I accomplish this on internal app routes in Laravel 5.1?

Essentially, I need to attach an 'Authorization' header to certain Requests. (i.e.

$request->headers->set('Authorization', 'my-authorization-token'); 

)This line of code does not work, however. No matter where I put it. It doesn't work from middleware. It doesn't work from routes.php. It doesn't work from my controllers... it just simply does not work period. (For the sake of clarity, '$request' is 'Illuminate\Http\Request').

What am I missing? Where/How can I set request headers before a request is sent? Please help! Thanks in advance.

DonnaJo
  • 538
  • 6
  • 15

3 Answers3

0

Some of the answers here might give you an idea, you could adapt them for the request: Where can I set headers in laravel

This also looks relevant: Laravel 5 / Lumen Request Header?

Community
  • 1
  • 1
Will Sheppard
  • 3,272
  • 2
  • 31
  • 41
  • Thanks for responding, @Will Sheppard. Unfortunately, I have already gone over both of those SO posts... the first one is actually for Response headers (which I already have handled), and the second one is about retrieving info from the Request header, not setting it. I appreciate your assistance, though. :-) – DonnaJo Oct 13 '15 at 15:15
0

The request is sent from the client to the server (i.e. your Laravel app). So you set the request headers on the client site using Javascript.

The Laravel documentation has an example of setting the X-CSRF-TOKEN header using jQuery.

$.ajaxSetup({
        headers: {
            'X-MY-HEADER': 'whateveryouwant
        }
});

Using VueJS it would look like this

Vue.http.headers.common['X-MY-HEADER'] = 'whateveryouwant';
mniess
  • 927
  • 11
  • 18
  • Thanks @mniess. I appreciate the help. I actually already have my basic AJAX (and Vue) request headers set up. That's all good. JS is not the issue. What I am trying to figure out is how to set the request headers from within the Laravel app, on internal requests that do not involve AJAX. That's where I'm stumped. Thank you, though. :-) – DonnaJo Oct 14 '15 at 04:53
  • If you need to manipulate Request headers, you need to use Javascript. Maybe you are working towards a dead end? What are you trying to accomplish? What do you need the header for? – mniess Oct 14 '15 at 05:59
  • Yes, @mniess, I am beginning to think the 'dead end' scenario is the most likely case here. I need the header in order to serve a token (in requests to my app), and I need to be able to send that token no matter where I'm sending the request from (i.e., a Vue instance OR an internal route that is not served from my Vue pages). This would be easy if I were not making an internal request, as I could set my headers if I were using Curl from an outside domain, and it's easy when I'm using Vue, but ... apparently... it's impossible otherwise, which seems weird, but true. – DonnaJo Oct 14 '15 at 06:09
  • But where do you get that token? Isn't that available internally anyway? – mniess Oct 14 '15 at 10:52
  • And again. The client is responsible for setting headers. You can't set headers on a simple link or a form request unless you install a browser plugin or use javascript. The webrowser as a client usually doesn't allow for setting your own headers. I'm guessing you're using this for JSON Web Tokens. Those are for authorizing a js frontend against a node backend (i.e. socket.io). Your regular requests should already be authorized with Laravels builtin authorization. – mniess Oct 14 '15 at 10:58
-1

You need to create a new request object and then set the header like this:

// e.g., Inside controller method

$request = new \Illuminate\Http\Request();
$request->setMethod('POST'); // or whatever your request type is
$request->header('Authorization', 'my-authorization-token');
CSSBurner
  • 1,565
  • 15
  • 13