请求,携带了自定义的请求头
GET http://localhost:8081/test
Content-Type: application/json; charset=utf-8
X-Platform: www
X-Version: 0.0.1
路由1
如果直接取header,不传入参数,会取出所有header,而且是一个数组
Route::get('/test', function (\Illuminate\Http\Request $request) {
$headers = $request->header();
return response()->json($headers);
});
返回结果
{
"content-type": [
"application\/json; charset=utf-8"
],
"x-platform": [
"www"
],
"x-version": [
"0.0.1"
],
"user-agent": [
"Apache-HttpClient\/4.5.10 (Java\/11.0.6)"
],
}
路由2
传入X-Version,此时只能用小写x-version 获取
Route::get('/test', function (\Illuminate\Http\Request $request) {
$headers = $request->header();
return response()->json($headers['x-version']);
});
[
"0.0.1"
]
路由3
这应该是正常的获取方式
Route::get('/test', function (\Illuminate\Http\Request $request) {
$version = $request->header('X-Version');
return response()->json([
'version' => $version
]);
});
返回结果
{
"version": "0.0.1"
}
————————————————
版权声明:本文为CSDN博主「彭世瑜」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。