If you try to get a HTTP header via Request::header('HTTP_USER_AGENT') when the request was made through a test case, it always returns "NULL". If the request was made through the browser everything works fine.
I checked in the symfony source code wether the headers were set (when running the test).
var_dump($server):
array(4) {
'HTTP_HOST' =>
string(9) "localhost"
'HTTP_USER_AGENT' =>
string(19) "Symfony2 BrowserKit"
'HTTPS' =>
bool(false)
}
Are you passing a "server" array when making the test action call? Check out the method signature:
/**
* Call the given URI and return the Response.
*
* @param string $method
* @param string $uri
* @param array $parameters
* @param array $files
* @param array $server
* @param string $content
* @param bool $changeHistory
* @return \Illuminate\Http\Response
*/
public function call()
Of course, I even could dump the $server array (with my custom headers) in the symfony source code.
Have you tried it and got it working?
@schickling HTTP_USER_AGENT is not an actual header, it is just a convenience variable that you can access through the $_SERVER array and that is extracted from value of the User-Agent header.
Have you tried using Request::header('User-Agent') instead? This is returning a value both in a browser as well as in a test suite.
Thanks @raphaelsaunier for your response. That actually really works for "User-Agent" but not for custom headers.
I'm still not able to "reach" custom headers. They are dropped somewhere...
@schickling, I was actually running into the same issue when I tried to set/retrieve an Authorization header in my tests.
After reading through the source of Symfony's Request class I found an overrideGlobals() method which apparently expects all headers to be prefixed with HTTP_. While I'm still unsure of how Symfony calls this method internally, prefixing your headers in that way seems to do the trick.
So, in your tests you'd make the following call:
$this->call('GET', '/api', array(), array(), array("HTTP_CUSTOM"=>"custom header"));
鈥nd you'd retrieve that custom header in your Laravel app with:
Request::header('Custom');
Thanks again @raphaelsaunier it works!
But I'm very insecure about the fact that "standard" requests (sent via browser, curl, ...) work like that:
"CUSTOM"=>"custom header"
Request::header('Custom');
And test requests (sent via $this->call(...)) work like that:
"HTTP_CUSTOM"=>"custom header"
Request::header('Custom');
Looks very inconsistent to me. Perhaps @taylorotwell knows more?
@taylorotwell Can you say something concerning my last comment. That issue isn't solved for me yet :(
@schickling, this is clearly a Symfony issue/feature. If you look at their tests, you'll see that this is exactly how they expect to pass and retrieve custom HTTP headers: Symfony/Component/HttpFoundation/Tests/RequestTest.php#L44-L45
Hi guys,
I'm facing the same problem I think, I make a call
$this->call('PUT', 'some/uri', array(... => ...));
In my controller that catches the URI however, my request method using both $request->getMethod and ->getRealMethod bring up "GET" as value... while I expect it to be a PUT. Any tips on this?
Just for the sake of community, I found the cause of my issue. I requested an instance of Request in my controller through Request::createFromGlobals(), which resulted in my HTTP Method that I called by using $this->action() in my test not being propagated. So apparently the global variables fail (not sure if this is normal behaviour) to get my HTTP Method I gave with the request, Route::getRequest() did however and thus solving my problem.
Hi!
I use AngularJS with Laravel, and have had problems to get the token with
$token = Request::header('_token'); // Not working
But I found out that you can't use underscore '_'. So:
$token = Request::header('mytoken'); // Working :-)
Info:
make a script in index.blade.php to make a constant with the csrf token:
angular.module("myApp").constant("CSRF_TOKEN", '<?php echo csrf_token(); ?>');
in app.js:
angular.module('myApp', [].......).run(function($http,CSRF_TOKEN) {
console.log('CSRF_TOKEN: ' + CSRF_TOKEN);
$http.defaults.headers.common['mytoken'] = CSRF_TOKEN;
})
Thanks @raphaelsaunier :+1:
@raphaelsaunier Your solution worked for me too :)
@raphaelsaunier 's comment worked for me as well
@raphaelsaunier saves the day
Most helpful comment
@schickling, I was actually running into the same issue when I tried to set/retrieve an
Authorizationheader in my tests.After reading through the source of Symfony's Request class I found an overrideGlobals() method which apparently expects all headers to be prefixed with
HTTP_. While I'm still unsure of how Symfony calls this method internally, prefixing your headers in that way seems to do the trick.So, in your tests you'd make the following call:
鈥nd you'd retrieve that custom header in your Laravel app with: