arsha
arsha

Reputation: 27

$request->header('Authorization') return null using laravel

public function productData(Request $request)
{
    $product_no = $request->product_no;
    $response = array();
    if($request->product_no)
    {
        try 
        {
            $header = $request->header();
            $Authorization = $request->header('Authorization');
            dd($Authorization);
            if (empty($Authorization)) 
            {
                $data = "Authorization key invalid";
                return $data;
            } 
            else 
            {
                $sp_token = str_replace("Bearer ", "", $Authorization);
                $varify_token = 'ZQWmRjUyNDJDQUFGRjBBOUMzMUZGQUVEOTA4QkYzOEU2RENBNEQ4OTIwMzRGQzY1NDA0QzIyMjk3RkJENkRzdsg=====';
                if($sp_token == $varify_token)
                {
                    $response = array(
                        'status' => 200,
                        'message' => 'success',
                    );
                }
                else 
                {
                    $response = array(
                        'status' => 300,
                        'message' => 'Invalid Bearer Token',
                    );
                }
            }
        } 
        catch (\Exception $e) 
        {
            $response = array(
            'status' => 500,
            'message' => 'Data Not Found',
            'msgErr' => 'Something went wrong',
            'result' => '0');
        }
    }
    else 
    {
        $response = array(
            'status' => 0,
            'message' => 'product Number Empty',
        );
    }
    return response()->json($response);
}

Above code working in local server and when print $Authorization then it return Bearer Token but same code not working on production server when I hit API using production URL using postman then it return Authorization key invalid and when I print or dd $Authorization then it return null. I don't know why?. Please help me.

Thank you

Upvotes: 2

Views: 1860

Answers (2)

Lokendra
Lokendra

Reputation: 56

Any route or url that uses Auth() must be encapsulated in the web middleware.

Let me explain using e.g

Route::group(['middleware' => '['web','auth']', function () {

Auth::user(); //Other auth routes

});

Upvotes: 0

robvankeilegom
robvankeilegom

Reputation: 168

The Authorization header might be stripped by your webserver or reverse proxy. Try renaming it to anything that isn't a default HTTP header. For example: Token.

Upvotes: 2

Related Questions