Are you getting any errors in the console? If so, please post it here
No data coming from API
I am practicing APi, i bulid 2 websites( jobs website, test website) in my local server, I want to take job data from first website and show them in my second test web site. I ran both of them The port of jobs website is http://127.0.0.1:8000 The port of test website is http://127.0.0.1:8080
The route in my api jobs website i
Route::middleware('auth:api')->group(function () {
Route::get('/jobs3', [JobApiController::class,'apijob']);
});
Controller:
public function apijob()
{
$jobs=Job::all();
// return view ('jobs',compact('jobs'));
return $this->sendResponse(JobsResource::collection($jobs), 'jobs Retrieved Successfully.');
}
public function login(Request $request)
{
$input = $request->only(['email', 'password']);
$validate_data = [
'email' => 'required|email',
'password' => 'required|min:8',
];
$validator = Validator::make($input, $validate_data);
if ($validator->fails()) {
return response()->json([
'success' => false,
'message' => 'Please see errors parameter for all errors.',
'errors' => $validator->errors()
]);
}
if (auth()->attempt($input)) {
$token = auth()->user()->createToken('passport_token')->accessToken;
return response()->json([
'success' => true,
'message' => 'User login succesfully, Use token to authenticate.',
'token' => $token
], 200);
} else {
return response()->json([
'success' => false,
'message' => 'User authentication failed.'
], 401);
}
}
public function logout()
{
$access_token = auth()->user()->token();
// logout from only current device
$tokenRepository = app(TokenRepository::class);
$tokenRepository->revokeAccessToken($access_token->id);
// use this method to logout from all devices
// $refreshTokenRepository = app(RefreshTokenRepository::class);
// $refreshTokenRepository->revokeRefreshTokensByAccessTokenId($$access_token->id);
return response()->json([
'success' => true,
'message' => 'User logout successfully.'
], 200);
}
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'required',
'con_id' => 'required',
'c_password' => 'required|same:password',
]);
if($validator->fails()){
return $this->sendError('Validation Error.', $validator->errors());
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$success['token'] = $user->createToken('MyApp')->accessToken;
$success['name'] = $user->name;
in the test website : route web Route::get('/Applyforjobs',[ConsumeApi::class,'index'])->name('Aman'); and controller:
public function index(){
$client = new Client();
$url = "http://127.0.0.1:8000/api/jobs3";
$params = [
//If you have any Params Pass here
];
$headers = [
'api-key' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.
];
$response = $client->request('GET', $url, [
// 'json' => $params,
'headers' => $headers,
'verify' => false,
]);
$responseBody = json_decode($response->getBody());
// dd($responseBody);
return view('jobs', compact('responseBody'));
}
The error : no data is coming from api when I dd($responseBody);
@Keshi Thx sir
I solved it the problem was in controller In the controller header I have to add Authorization in stead of api _key
Please or to participate in this conversation.