2

why laravel doesn't accept vars with underscore from request header?

I made a simple request example with a variable: "token_auth" with value 123 , but inside in my route doesn't get this value.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;


class Test extends Controller
{

    public function teste(Request $request){

        dd($request->header());

    }

}

only prints header with no underscore:

array:9 [
  "thisheadernounderline" => array:1 [
    0 => "312321"
  ]
  "cache-control" => array:1 [
    0 => "no-cache"
  ]
  "postman-token" => array:1 [
    0 => "3c461fd1-5bea-4100-9926-81c14cb5810c"
  ]
  "user-agent" => array:1 [
    0 => "PostmanRuntime/7.1.1"
  ]
  "accept" => array:1 [
    0 => "*/*"
  ]
  "host" => array:1 [
    0 => "localhost"
  ]
  "cookie" => array:1 [
    0 => "XSRF-TOKEN=eyJpdiI6IkJwM3pjVkFBb2hxS2d4MDFcL2srM0h3PT0iLCJ2YWx1ZSI6IiszRzhoTzV0VzN5YUkydUNUTGR5aENVd291ZW01SkZ4V2ZxQkNDTGJwbDlyMFFJZGxzNnorMkF0VUNTbHpoRndLV3FmbndJWFhkXC9cL3IzOGZvN25zN3c9PSIsIm1hYyI6IjQwZWQ1YmJhM2VjM2I3N2RiNWZlYjcwYjZmYzQ0NDk5YjkwZDc4YzRjNGQwZjQxNDVkOGU1NDU0MTA0OWI2YWYifQ%3D%3D; laravel_session=eyJpdiI6IitSckpmOFI1TmpuXC9SSUt2QVY3VlFRPT0iLCJ2YWx1ZSI6IlwvVk1EaDdYdDNxRTZLNytRcnZDTlNiaVlFTWVRVmNUOHlyVnFia0pDeE9HNWpNa3QrWlBsNnNoVEduVkhrMUhkYURoNDI4cW9RdXVHU0lIS0JZN2REQT09IiwibWFjIjoiNWJmYmJmNTdmMzJkZjQ1OGQ4NTM1NjhhMzQxNDk5NWUxOTA5OGVjOThkODkyNDgwZTA2NzEyYjFlZmE2YjVjOSJ9"
  ]
  "accept-encoding" => array:1 [
    0 => "gzip, deflate"
  ]
  "connection" => array:1 [
    0 => "keep-alive"
  ]
]

is there any workaround to solve this?

And I cant change this because this variable comes from an API (aready asked to change but they wont wanna change).

Already tried with laravel 5.4 and laravel 5.6.

ps: with simple php works fine (no laravel framework)

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
sealabr
  • 1,593
  • 3
  • 19
  • 28
  • 6
    Possible duplicate of [Why underscores are forbidden in HTTP header names](https://stackoverflow.com/questions/22856136/why-underscores-are-forbidden-in-http-header-names) – Daniel Krom Apr 26 '18 at 12:48
  • but using only php works fine... also i'm using apache – sealabr Apr 26 '18 at 12:54
  • 2
    Yea, read the link: http://httpd.apache.org/docs/trunk/new_features_2_4.html `Headers containing invalid characters (including underscores) are now silently dropped.` If you control the headers, just dont use underscore – Daniel Krom Apr 26 '18 at 13:26
  • 1
    The answer to *why* is because it's against HTTP protocol specification. Now that you know, use a hyphen character instead of underscore and your problem goes away. Don't battle the protocol, simply respect it and your life will be good. – N.B. Apr 26 '18 at 14:29

1 Answers1

0

This worked for my scenario.
I used this workaround in Laravel to solve my problem:

foreach (getallheaders() as $name => $value) {
    echo "$name: $value\n";
}

output:

token_auth: 123
cache-control: no-cache
Postman-Token: f5bf7878-1f64-4ee8-907a-991d73ab8667
User-Agent: PostmanRuntime/7.1.1
Accept: */*
Host: localhost
cookie: XSRF-TOKEN=eyJpdiI6IkJwM3pjVkFBb2hxS2d4MDFcL2srM0h3PT0iLCJ2YWx1ZSI6IiszRzhoTzV0VzN5YUkydUNUTGR5aENVd291ZW01SkZ4V2ZxQkNDTGJwbDlyMFFJZGxzNnorMkF0VUNTbHpoRndLV3FmbndJWFhkXC9cL3IzOGZvN25zN3c9PSIsIm1hYyI6IjQwZWQ1YmJhM2VjM2I3N2RiNWZlYjcwYjZmYzQ0NDk5YjkwZDc4YzRjNGQwZjQxNDVkOGU1NDU0MTA0OWI2YWYifQ%3D%3D; laravel_session=eyJpdiI6IitSckpmOFI1TmpuXC9SSUt2QVY3VlFRPT0iLCJ2YWx1ZSI6IlwvVk1EaDdYdDNxRTZLNytRcnZDTlNiaVlFTWVRVmNUOHlyVnFia0pDeE9HNWpNa3QrWlBsNnNoVEduVkhrMUhkYURoNDI4cW9RdXVHU0lIS0JZN2REQT09IiwibWFjIjoiNWJmYmJmNTdmMzJkZjQ1OGQ4NTM1NjhhMzQxNDk5NWUxOTA5OGVjOThkODkyNDgwZTA2NzEyYjFlZmE2YjVjOSJ9
accept-encoding: gzip, deflate
Connection: keep-alive
sealabr
  • 1,593
  • 3
  • 19
  • 28