Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sevenTopo's avatar

Laravel Rest api: this request has no response data available

hi developers . it's my first time with rest api ,using laravel as a backend .

so the probleme is that i can't achieve to return my data ($users) for exemple.,it returns this message in

browser console "this request has no response data available".

so first this is my route in routes/api.php :

Route::middleware('jwt.auth')->get('users', function () {
    return auth('api')->user();
});

Route::post('/testlogin', 'Api\TestController@login'); //this is my route 
  

for my controller app/http/controllers/Api/TestController :

     public function login()
    {  


    try {
        $postdata = json_decode(file_get_contents("php://input"), true);
        $credentials['email'] = $postdata['email'];
        $credentials['password'] = $postdata['password'];
         if (!$token = auth('api')->attempt($credentials)) {
        return response()->json(['error' => 'Unauthorized'], 401);
             }
             $data=array();
             $data['token']=$token;
             $data['expires']=auth('api')->factory()->getTTL() * 60;

             //when i return response()->json($data['token']); it show's the the token but when i return response()->json(['data'=>$data]); no response at all.
             return response()->json($data);
    } catch (Exception $e) {
         return response()->json("error");
    }  
    
    }

0 likes
11 replies
bugsysha's avatar

Have you tried anything like:

return response()->json(['something'=>$data]);

Just to make sure you are not using some reserved key?

Also what do you have at frontend?

sevenTopo's avatar

yes i did but no response att all . the only way is to use Response()->json($emailOrPasswordNotanObject);

bugsysha's avatar

Are you sure that you do not have some kind of a plugin/extension in your browser which causes this issue? Have you tried private/incognito mode?

sevenTopo's avatar

Yes im sure ,it works fine with postman ;i changed the browser to mozila but im facing the same probleme .

im not sure if the probleme is releated to cors plocies . i added

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');
header('Content-Type: application/json');
class UserController extends Controller
{
 public function login()
{
////
}
}

to my controller . but no results. i'm struggling with it .

bugsysha's avatar

In the routes you've posted this

Route::post('/testlogin', 'Api\TestController@login');

And in post from hour ago, you posted

class UserController extends Controller
{
    public function login()
    {
    ////
    }
}

So is it TestController or UserController. Did you mistyped your controller in routes?

sevenTopo's avatar

oh sorry don't worry about it .that is not the problem

bugsysha's avatar

You probably missed something. What you've shown here should work. Probably some piece of code you have in your project is causing this issue and without showing it here we can only guess, but that is too much ground to cover.

sevenTopo's avatar

Thank you so much for your help i fixed the probleme .i created a BaseController .

<?php

namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as Controller;

class BaseController extends Controller
{
public function sendResponse($result , $message){
$response = [
'success' => true ,
'data' => $result,
'message' => $message
];
return response()->json($response , 200);
}
}

and in my UserController or any X controller you i did that :



namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Api\BaseController as BaseController;
use App\Role;
use App\User;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
    header('Access-Control-Allow-Methods : POST, GET, OPTIONS, PUT, DELETE, HEAD');
    header('Allow: POST, GET, OPTIONS, PUT, DELETE, HEAD');
    header('Access-Control-Allow-Headers : X-Requested-With, Content-Type');
class UserController extends BaseController
{
    
    public function authenticate(Request $request)
    {   
         $credentials = json_decode($request->getContent(),true);
        //$credentials = $request->only('email', 'password');
        try {
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 400);
            }
        } catch (JWTException $e) {
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        $user = User::where('email',$credentials['email'])->first();
        $roles=Role::where('user_id',$user->user_id)->get();
        if($user)
        {
         return $this->sendResponseLogin($token,'success',$user,$roles);
        }else{
         return $this->sendError('invalid_credentials','mot de passe ou login erroné');
        }

}

}

so this version of code it works it return a response with json data ..why ?? im not sure .

bugsysha's avatar

Move headers to response or create middleware.

Please or to participate in this conversation.