5

So the question is simple:

How to send custom request header, when I testing with Laravel?

I'm trying to make like so:

$this->call('POST', '/my/route', ['params' => 'array'], [], ['X-Custom' => 'header']);

But when I call Request::header('X-Custom') in my controller, I didn't get it. Yes, it's available in Request::server('X-Custom'), but it's not what I need.

So I need to get it in Request::header().

PS: Laravel 4

MrSil
  • 608
  • 6
  • 12

3 Answers3

14

You need to properly form the header, or it will be ignored. Try this:

this->call('POST', '/my/route', ['params' => 'array'], [], ['HTTP_X-Custom' => 'header']);

The HTTP_ will be stripped when you look at your Request

Laurence
  • 58,936
  • 21
  • 171
  • 212
1

TLDR; Prefix your custom headers with 'HTTP_'

If anybody is interested in why this worked, read this issue:

https://github.com/laravel/framework/issues/1655

Visti K
  • 225
  • 3
  • 4
0

In Laravel >= 5.1, the call method based on its definition:

call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)

needs an extra array before headers array:

this->call('POST', '/my/route', ['params' => 'array'], [], [], ['HTTP_X-Custom' => 'header']);
Maverick
  • 21
  • 6