PHP: Laravel gets the request header header

Request, carrying a custom request header

GET http://localhost:8081/test
Content-Type: application/json; charset=utf-8
X-Platform: www
X-Version: 0.0.1

route 1

If you take the header directly without passing in parameters, all the headers will be taken out, and it is an array

Route::get('/test', function (\Illuminate\Http\Request $request) {
    
    
    $headers = $request->header();

    return response()->json($headers);
});

return result

{
    
    
  "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)"
  ],
  
}

route 2

Incoming X-Version, at this time only lowercase can be used x-versionto obtain

Route::get('/test', function (\Illuminate\Http\Request $request) {
    
    
    $headers = $request->header();

    return response()->json($headers['x-version']);
});
[
  "0.0.1"
]

Route 3

This should be the normal way to get

Route::get('/test', function (\Illuminate\Http\Request $request) {
    
    
    $version = $request->header('X-Version');

    return response()->json([
        'version' => $version
    ]);
});

return result

{
    
    
  "version": "0.0.1"
}

Guess you like

Origin blog.csdn.net/mouday/article/details/130933313