Laravel Eureka client error 404 not found Response - laravel

I have error!!! Please help!!!
GuzzleHttp\Exception\ClientException : Client error: POST http://10.100.0.7:8080/eureka/apps/NewsService resulted in a 404 Not Found response
public function handle()
{
$client = new EurekaClient([
'eurekaDefaultUrl' => 'http://10.100.0.7:8080/eureka-
server/eureka',
'hostName' => 'NewsService',
'appName' => 'NewsService',
'ip' => '10.111.2.23',
'port' => ['8000', true],
]);
try {
$client->register();
$client->start();
} catch (Exception $e) {
return $e->getResponse();
}
}

Create laravel console command and use https://github.com/PavelLoparev/php-eureka-client

Related

Flutter and Laravel API. File upload. The GET method is not supported for this route. Supported methods: POST

I want to upload the image from Flutter. But I am getting this error:
The GET method is not supported for this route. Supported methods:
POST.
But I set my api route as POST method. And I also sending POST method request, but still I am getting this error.
But one more thing, it works on POSTMAN and INSOMNIA. There is no problem.
I use this header:
Content-Type: multipart/form-data
Authorization: ....
Please help me.
My route is:
Route::post('/avatar/update', 'Api\ProfileController#avatar_update')->name('api.avatar.update');
My controller:
public function avatar_update(Request $request){
$request->validate(array(
'avatar' => 'required|image',
));
try{
$image = Image::make($request->avatar)->fit(250, 250);
$photo_name = Auth::user()->username."-".strtotime(now()).'.'.$request->avatar->extension();
$path='images/avatars/'.$photo_name;
$image->save($path);
if(File::exists(Auth::user()->avatar)) {
File::delete(Auth::user()->avatar);
}
Auth::user()->update([
'avatar' => 'http://attendance.isadma.com/'.$path,
]);
return response()->json([
'status' => 1,
'message' => 'Picture updated.',
'image' => Auth::user()->avatar
], 200);
}
catch (\Exception $e){
return response()->json([
'status' => 0,
'message' => $e->getMessage()
], 500);
}
}
Flutter request code:
#override
Future<String> uploadProfilePic(File profilePic, String token) async {
var postUri = Uri.parse("$url/avatar/update");
print(profilePic.path);
print(postUri);
var request = http.MultipartRequest("POST", postUri);
request.headers['authorization'] = "Bearer $token";
request.headers['Content-Type'] = "multipart/form-data";
request.files.add(
await http.MultipartFile.fromPath(
'avatar',
profilePic.path,
contentType: MediaType('image', 'jpg'),
filename: basename(profilePic.path),
),
);
print(request.headers);
request.send().then((res) async {
print(res.headers);
print(res.statusCode);
print(await res.stream.bytesToString());
}).catchError((e) {
print(e);
});
}
make sure you are sending the csrf data (_token) in your post request

Client error: `POST https://graph.facebook.com/v3.0/oauth/access_token` resulted in a `400 Bad Request` [duplicate]

This question already has answers here:
Laravel Socialite Facebook error: "POST https://graph.facebook.com/oauth/access_token resulted in a `400 Bad Request` " [duplicate]
(3 answers)
Closed 3 years ago.
Client error: POST https://graph.facebook.com/v3.0/oauth/access_token resulted in a 400 Bad Request
response: {"error":{"message":"\u05d0\u05d9\u05df
\u05d0\u05e4\u05e9\u05e8\u05d5\u05ea \u05dc\u05d8\u05e2\u05d5\u05df
\u05d0\u05ea (truncated...)
install socialite package in laravel
public function Callback(Request $request, $provider)
{
try {
$userSocial = Socialite::driver($provider)->stateless()->user();
}
catch (Exception $e) {
return redirect ('/');
}
if (!$request->has('code') || $request->has('denied')) {
return redirect('/');
}
//$userSocial = Socialite::driver($provider)->stateless()->user();
$users = User::where(['email' => $userSocial->getEmail()])->first();
if($users){
Auth::login($users);
return redirect('/home');
}
else
{
$user = User::create([
'name' => $userSocial->getName(),
'email' => $userSocial->getEmail(),
'image' => $userSocial->getAvatar(),
'provider_id' => $userSocial->getId(),
'provider' => $provider,
]);
return redirect()->route('/home');
}
Any help is really appreciated.
You can use this php sdk (https://github.com/facebook/php-graph-sdk) .It supports from php version 5.4.

Laravel Storage Ftp returns Could not login with connection

I am using Storage::createFtpDriver. But I want to validate the credentials before proceeding.
If I do $ftp->exists(self::REMOTE_FILE_DEST) then I am getting the error, I would like boolean instead, so I can work with it.
Error I am getting looks:
Could not login with connection: xxxxxxx
Code I currently have is:
$ftp = Storage::createFtpDriver([
'host' => getSetting(SettingRepository::FTP_HOST)->getvalue(),
'username' => getSetting(SettingRepository::FTP_USERNAME)->getvalue(),
'password' => getSetting(SettingRepository::FTP_PASSWORD)->getvalue(),
'port' => getSetting(SettingRepository::FTP_PORT)->getvalue(),
'timeout' => getSetting(SettingRepository::FTP_TIMEOUT)->getvalue(),
]);
Catch exception and return true/false on that.
try {
Storage::createFtpDriver([...]);
return true;
} catch (Exception $e) { // If I looked correctly it is RuntimeException so you can be more explicit
return false;
}

Laravel 5.5 - Handle Error Exception for 'where'

In Laravel 5.5 I am trying to handle an error exception like this...
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
}
catch(ModelNotFoundException $e) {
return Response::json(array(
'error' => true,
'status_code' => 400,
'response' => 'fruit_id not found',
));
}
But this is giving me a 'Trying to get propert of non-object' error
The same error handling works correctly for findorfail, how should I be doing this for the 'where' statement?
I think you are passing wrong values in your where query in the try block.
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
}
Is it fruit_id or just id because you are querying it on fruit model itself.
Thanks to some pointers in the comments I changed to
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
}
catch(\Exception $e) {
return Response::json(array(
'error' => true,
'status_code' => 400,
'response' => 'fruit_id not found',
));
}
All is now working now I am catching the correct exception
Tested on Laravel 5.7
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
}
catch(\Exception $e) {
abort(404);
}
Even though you got it working, I'd like to make mention of Laravel's Exception Handler.
The report method allows you to catch any exception type and customize how you wish to process and move forward. There is a report helper function as well which is globally accessible.
Furthermore, reportable and renderable exceptions allow you to create and customize your responses.
Try the below code:
if ($user->fruit) {
try {
$fruits = Fruit::where('fruit_id', $user->fruit->id)->get();
} catch(\Exception $e) {
return Response::json(array(
'error' => true,
'status_code' => 400,
'response' => 'fruit_id not found',
));
}
} else {
return "User Fruit doesn't exists!!";
}

How do I handle route exception not defined?

I created dynamic sidebar menu and when I try to insert new menu I am getting error message Route [nameroute] is not defined. How do I handle this error with try catch ?
This is my controller file.
DB::beginTransaction();
try
{
$insert = AppMenu::insertGetId([
'description' => $request->description,
'menu_url' => $request->menu_url ? $request->menu_url:null,
'menu_alias' => $request->menu_alias ? $request->menu_alias:null,
'ismenu' => $request->ismenu,
'parent' => $request->parent ? $request->parent:null,
'menu_icon' => $request->menu_icon,
'menu_order' => $request->menu_order
]);
DB::table('appmenu_role')->insert([
'appmenu_id' => $insert,
'role_id' => $role
]);
}
catch (\InvalidArgumentException $e)
{
return Redirect::back()->with('infoMessage', "Route not defined. ");
}
DB::commit();
Session::flash('successMessage', 'Menu Created');
return Redirect('menu');
You should use Exception class to catch any kind of exception.
catch (\Exception $e)
{
return Redirect::back()->with('infoMessage', "Route not defined.");
}

Resources