Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arnekolja's avatar

Request headers not arriving when set for JSON requests while testing

Hi folks,

I am currently struggling with setting headers in my Laravel test cases. I am adding custom headers, but when debugging the request from within my application, they are not set at all:

<?php

namespace Tests\Feature\API;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SearchTest extends TestCase
{

    use RefreshDatabase;

    /**
     * Search for my custom value with app starting with version 2.1.0.
     *
     * @return void
     */
    public function test_search_for_my_custom_value_with_app_from_v2_1_0()
    {
        $this->seed();

        $user = User::factory()->create();

        $params = [
            'custom_array' => ['custom_value']
        ];

        $headers = [
            'X-App-Version' => '2.1.0 b1'
        ];

        $response = $this->actingAs($user)->postJson('/api/user/search', $params, $headers);
    }

}

When debugging either request()->headers or getallheaders() my custom header isn't included:

object(Symfony\Component\HttpFoundation\HeaderBag)#5205 (2) {
    ["headers":protected]=>
    array(5) {
      ["host"]=>
      array(1) {
        [0]=>
        string(17) "domain.com"
      }
      ["user-agent"]=>
      array(1) {
        [0]=>
        string(7) "Symfony"
      }
      ["accept"]=>
      array(1) {
        [0]=>
        string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
      }
      ["accept-language"]=>
      array(1) {
        [0]=>
        string(14) "en-us,en;q=0.5"
      }
      ["accept-charset"]=>
      array(1) {
        [0]=>
        string(30) "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
      }
    }
    ["cacheControl":protected]=>
    array(0) {
    }

When calling the same endpoint from my mobile application, the headers are correct. Also, as you can see, the accept header is wrong. Somehow the request headers don't arrive at all.

As far as I can see there is no redirect involved or something. My REST debugger is advised to not follow redirects just to make sure I'm debugging correctly.

Does anyone have an idea what I am missing here?

edit: I also tried using withHeaders() instead of passing it as parameter to postJson().

0 likes
1 reply
arnekolja's avatar

I just solved it on my own. The problem was indeed somewhat different.

I had a custom cast on an attribute that checked the header. At some point in my test code I was setting this attribute and when saving the object, it ran the cast and got no headers, as it was the request of the test itself and not the one of the postJson() call.

I solved it by using setRawAttributes() on the object inset of setting it like normal and implicitly calling the cast.

However, it would be interesting how to inject the request headers for the test itself.

Please or to participate in this conversation.