首页 > 解决方案 > Laravel 自定义请求标头未填充

问题描述

我正在使用邮递员向我的项目发出请求,如下所示:

Request Headers:
cache-control:"no-cache"
postman-token:"65b35825-8c35-47ae-ad03-159d3da08e95"
partner_key:"123456789"
partner_secret:"123456789"
business_external_id:"123456789"
user-agent:"PostmanRuntime/6.4.1"
accept:"*/*"
host:"loyaltybro.local"
accept-encoding:"gzip, deflate"

我对“partner_key”、“partner_secret”、“business_external_id”感兴趣。

在我的代码中,我正在记录收到的标头,如下所示:

public function handle($request, Closure $next)
{   
    $headers = $request->headers->all();
    \Log::info($headers);
    ...
}

这是日志输出。

local.INFO: array (
  'cache-control' => 
  array (
    0 => 'no-cache',
  ),
  'postman-token' => 
  array (
    0 => '65b35825-8c35-47ae-ad03-159d3da08e95',
  ),
  'user-agent' => 
  array (
    0 => 'PostmanRuntime/6.4.1',
  ),
  'accept' => 
  array (
    0 => '*/*',
  ),
  'host' => 
  array (
    0 => 'loyaltybro.local',
  ),
  'accept-encoding' => 
  array (
    0 => 'gzip, deflate',
  ),
  'connection' => 
  array (
    0 => 'keep-alive',
  ),
) 

没有“partner_key”、“partner_secret”、“business_external_id”。

为什么他们没有被填充?

标签: phplaravel

解决方案


默认情况下,在 nginx 和 apache 中都会删除带有下划线的标头。

http://httpd.apache.org/docs/trunk/new_features_2_4.html

将标头转换为环境变量比以前更严格,以通过标头注入减轻一些可能的跨站点脚本攻击。包含无效字符(包括下划线)的标题现在被静默删除。

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/?highlight=disappearing%20http%20headers#missing-disappearing-http-headers

如果你没有明确地设置 underscores_in_headers on;,NGINX 将默默地丢弃带有下划线的 HTTP 标头(根据 HTTP 标准这是完全有效的)。这样做是为了防止在将标头映射到 CGI 变量时出现歧义,因为在该过程中,破折号和下划线都映射到下划线。


推荐阅读