How to set Laravel to catch/log 500 errors from API?
I have Laravel 6.x and, in .env, I have APP_ENV=production with APP_DEBUG=false and a function with DB::table('table_doesnt_exists')->get().
If I access a web route from browser which call that function I see on the screen "500 Server Error" and the error is written in logs, which is perfect.
If use axios and call an API route which call the same function I only get HTTP response 500, without any response, and most important there is no error in logs.
I have manage to make it works after trying multiple combinations.
APP_ENV=local
APP_DEBUG=false
The error is written in logs and the API only return
{ message: "Server Error" }
Related
I am working in a Vue app with a Laravel api and I am getting exactly what it says in the title.
My Axios call: return await axios.post("/customers/load-cart-data", payload);
Inside the general section of my network tab: Request Method: POST
What I get back from our api: "message": "The GET method is not supported for this route. Supported methods: POST."
I even log out the req in an interceptor and it says: ....,method: "post"
I am just wondering if there is some server routes configuration something that is missing or if there is anything I can do to fix this.
Thank you all!
This could happens when you declare the same name of route for api.php and web.php with different method, for a api.php you should use post method and web.php should be routes for pages.
After change the route names on your api on the console write this command.
php artisan route:clear
with this you should clear all routes cache and after this try test your api.
This can happen when you mixing up your http and https or if you use the wrong domain extension.
This problem happened to me recently where I was using .com instead of .net
To confirm this is happening to you, inspect your network requests (if you have F12 web dev tools, then use the Network tab), then you will see that your POST request was returned with a 301. Then, this would be followed by a GET request to the same URL.
I have 2 servers which are API server and Client server ....
Both server using Google Cloud server and I use Laravel framework to develop my system...
So, currently the problem is, it return 403 error when calling API (to API server) using GuzzleHttp (from Client Server).....
But after I change the user agent to curl/7.65.3, suddenly it is working fine...
But I want to know why??? Is there any other solution without changing the user-agent???
Thanks
What is your use method? If GET you can refer to:
GET Requests That Include a Body
If a viewer GET request includes a body, CloudFront returns an HTTP status code 403 (Forbidden) to the viewer.
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/RequestAndResponseBehaviorCustomOrigin.html
This has happened way too often. I've cleared my local database by running php artisan migrate:fresh and after that, php artisan passport:install --force. For some obscure reason, I'm having this infernal error
MethodNotAllowedHttpException
when I try to access my auth:api routes only, since registration and login work.
These are my routes:
Route::middleware('auth:api')->group(function () {
Route::get('me', 'Api\UserController#details');
Route::get('profile/{username}', 'Api\UserController#getUsername');
Route::post('profile/{username}/trust', 'Api\TrustController#trust');
});
Route::post('register', 'Api\UserController#register')->name('register');
Route::post('login', 'Api\UserController#login')->name('login');
I am absolutely sure I am sending a GET request.
I know that this happens when I am not properly logged in, in other words, giving a correct token to the API, which is not the case now because I have logged in normally, and pasted the token in the header as usual.
What else? Before I refreshed my database it was working. After the refresh, this has started. Any ideas?
When you are sending this request laravel thinks that you are sending a web request instead of api request.
In order for laravel to know that its a api request set the headers:
Content-Type : application/json
Accept: application/json
On your postman request. Since based the 1 flag on your header I cann see that your headers are not set correctly.
I'm using guzzle and laravel to call an API however when I get an error from the API, I can't view the error message sent back from the API. Laravel / Guzzle gives a default error Client error: 400.
How do I view the error message sent back?
I added this to the request which solved my issue.
'http_errors' => false
I have a cordova app running on angular that uses a sails server as its API endpoint. It is set up to use CORS and everything is working with ajax calls.
I am attempting to connect to the sails server using socket.io. I copied the sails.io.s file from the sails server's assets folder to the cordova app. I modified it so that it connects to 127.0.0.1:1337 and does so successfully. It does not use cookies on the front end and the sails end does not use socket authorization (set in config/sockets.js).
When I call:
io.socket.get('/controller/action', function callback(return){
});
The return object looks something like this:
{
message: "TypeError: Object #<Object> has no method 'get'",
status: 500
}
The sails server log doesn't show that it is hitting /controller/action. Does anyone have any experience with using the custom sails io library on a JS client that isn't sails?