Skip to content
Advertisement

Laravel request url with parameter problem with GET Method

I have a route in api.php which look like this:

Route::get('auth/logout/{token}','UserController.php';

I tested this API endpoint using Postman with these configurations:

  • Method: GET
  • Params: key = token; value = $2y$10$Xji0VW1Qq9rtF04QlXDu1ePKNKHpRA2ppjDYWNFX.37C30sd3WSIu
  • Header: none
  • URL: localhost:8000/api/v1/logout?token=$2y$10$Xji0VW1Qq9rtF04QlXDu1ePKNKHpRA2ppjDYWNFX.37C30sd3WSIu

Here is my UserController@logout:

public function logout($token){
    return response()->json([
        'message' => 'Logout Success',
        'token' => $token
    ], 200);
}

As you can see there, I just want to show a message and the $token parameter in Postman. But my problem is, Postman shows me a blank response. I can’t access the URL with ? as the parameter separator. But I can access the URL with /, just like host/api/v1/auth/logout/{token_value}. But it is not what I desired. Anyone can help me?

Advertisement

Answer

You can remove the token route parameter:

Route::get('auth/logout', 'UserController.php');

And retrieve the token from the request in the controller:

public function logout(Request $request) {
    return response()->json([
        'message' => 'Logout Success',
        'token' => $request->token
    ], 200);
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement