301 Moved Permanently on post ajax requests in laravel - laravel

I upload a laravel project on server;
When I run all the ajax post request request run like this:
Request URL: http://example.com/user/register/
Request Method: POST
Status Code: 301 Moved Permanently
Then this run:
Request URL: http://example.com/user/register
Request Method: GET
Status Code: 500 Internal Server Error (from disk cache)
Post method change to get method and return error;
How can I solve this?!
I try php artisan cache:clear php artisan config:clear php artisan view:clear
But also return error

I found the answer. put here to help the others.
It take so time of me. The problem was so simple.
I just remove the backslash from end of url of each post actions.
For example /user/register/ must change to /user/register :|
And cache of the browser must be clear for each page.

In my case for uploading file, my route was same as public directory -> Route::POST('upload/data', [UploadDataController::class, 'upload'])->name('uploadData');
On my public folder:
public/upload/data
So I changed the route as upload/uploadData and its perfectly fine!

Related

Getting GET error while sending POST request to the POST route in Laravel API

Locally project runs fine. On the server getting this error while sending POST request to the POST route in Laravel 9 API.
"message": "The GET method is not supported for this route. Supported methods: POST."
My route from the api.php routes file:
Route::post('/userdata/create', [UserDataController::class, 'createAccount']);
My route from the routes list (from php artisan route:list):
POST api/v1/userdata/create .......... Api\V1\UserDataController#createAccount
Tried:
php artisan cache:clear
php artisan route:cache
php artisan route:clear
Didn't fixed it yet.
It happens cause you have forced your domain to HTTPS redirect and you are trying to use it as HTTP.
Your domain settings redirect your request to HTTPS but not to POST method.
If you look for postman logs/console you will find redirection of HTTP POST request to HTTPS GET request
Hence Error response by your server of
The solution to that problem was actually very simple:
I needed to send POST request to "https" url, and i was sending it to "http" url.
Still strange that server was complaining that GET method is used...
This problem happened to me recently where I was using .com instead of .net
that happen because laravel cannot return the error.
SOLUTION 1 :
you can use this inside your API post function :
try{
// example we have name field
$request->validate([
'name' => ['required','string','max:255']
]);
User::create([
'name' => $request->name
]);
return 'registered';
}catch(Exception $error){
// your error from validation will appear here
// dd($error->validator->messages());
}
SOLUTION 2 :
as alternative :
I found another method here at this link

How to debug unwanted 302 redirect from Ajax request?

I'm trying to get data from a database through this ajax request:
axios.get('/about-info')
web.php:
Route::get('/about-info', [CMSController::class, 'aboutInfo']);
CMSController.php:
public function aboutInfo()
{
$data = DB::table('about_info')->first('order by id desc');
return $data;
}
but instead I am getting the whole welcome.blade.php content. It looks like the url in web.php is not called and instead a redirect happens. The dev tools network tab shows a 302 redirect.
This thread seems to have insight on this issue. I've been trying to implement answer 3 (adding accept: 'application/json to the config/headers object of the request) but the object already has that entry:
config:
headers:
Accept: "application/json, text/plain, */*"
This guide is talking about auth middleware being the possible cause of this problem but I'm not using middleware (at least none I am aware of). Any idea how to get to the root of this?
Have you tried moving the route to your api routes file? All routes in the web namespace receive session state, CSFR, and possibly more that could be getting in the way (i.e. "new session, go to the welcome screen!").
This would change your URL path to:
axios.get('/api/about-info')
Turns out that web.php was not working at all because of caching issues. Running:
php artisan route:clear
made it work again and showed every problem, which could not be detected before clearing the cache.

Laravel 5.2 testing always returning 404

guys,
I'm having this issue to create my firsts tests on my project. I already try almost everything, search in forums, but I can't resolve this problem. Every page that I try to access using get in my test is always returning 404.
public function testExample()
{
$user = factory(User::class)->make();
$this->actingAs($user)->get('/book-original');
$this->assertResponseOk();
}
There was 1 failure:
1) FileTest::testExample Expected status code 200, got 404.
I'm using a separeted .env.testing file, but try to setup my .env file same as the testing, in case the Laravel is ignoring the testing and reading the main one, but the result is the same.
I try to use the $baseUrl and .env's APP_URL with http://localhost and my IP and try to use the return value from php artisan serve command, in my case, http://localhost:8000, but in both cases the error persists.
Important: I used php artisan config:cache every time I changed the configs.
I'm already lost. Has someone a hint to help here?
Thanks so much.

Laravel Socialite InvalidStateException in AbstractProvider.php line 200

I'm building a web app in my local system (Ubuntu-14.04 64Bit) using laravel 5.3. I used Socialite to signin from social networks. I configured G+, Facebook, GitHug. I'm using Chromium as my default browser. Finally the problem is i'm getting
InvalidStateException in AbstractProvider.php line 200
frequently. i tried
php artisan cache:clear
php artisan config:clear
composer dump-autoload
these are helping to solve the issue temporarily, again the problem raising.
please help me in this issue..
I have the same issue and I've read a lot about this, that depend if the URL where you are at the moment of the login request has www. at the beginning or not.
Into config\services.php, if you have the redirect set as http://sitename.tld/callback/facebook the oauth works if you send the login request from sitename.tld, while if you try from www.sitename.tld you get the exception.
I haven't yet understood how to have it working with and without www at the beginning.
If the AbstractProvider.php line 200 fires the exception when the state of the user is not present means that the User cannot be created.
First check your code when you get the details from the provider(facebook, github) if you create a user and you return it.
If you have managed and logged in your app and you deleted the user from the user table remember to delete also the data from the socialite account table.
I was getting that exception because 'state' wasn't saved in session. But I was using asPopup method - Socialite::driver('facebook')->asPopup()->redirect(); so I saved session then - $request->session()->save();. So I solved this issue.
or try
session()->put('state', $request->input('state'));
$user = Socialite::driver('facebook')->user();
it works
I have same issue and solved in 3 steps;
add request on the top
use Illuminate\Http\Request;
Pass request object to function
public function handleProviderCallback(Request $request)
{
try {
$user = Socialite::driver('facebook')->user();
} catch (Exception $e) {
throw new Exception;
}
}
Clear cache.
php artisan cache:clear
I had the same error but my solution was a little different. I am posting here just in case someone else keeps hitting this post like I did for a possible answer.
I develop on Ubuntu 18.04 desktop since it is a server with a GUI. Socialite works great locally but as soon as I pushed/pulled the changes through git to the server, it quit.
I was running traces by recording what was sent to and from google. I "dd($_GET)" to get a raw dump before Socialite had a chance to get the info so I knew what was stored and ready for use. All info was there but Socialite didn't seem to "see" it. That is when I reasoned it was my apache2 header configuration interfering with the cookies/session data.
I had set header security in my apache2 configs. One of the settings was
Header always edit Set-Cookie ^(.*) "$1;HttpOnly;Secure;SameSite=Strict"
This setting was interfering with the cookie information that socialite needed. I removed that setting from my apache2 header config(by commenting out) and restarted Apache. Finally I removed all sessions in storage/framework/session/* and cleared them from my browser just to be sure. That worked for me.
After I got it working, one by one enabled and tested each of the following settings to have the framework secure what header info it can:
SESSION_SECURE_COOKIE=true
in my .env file
'http_only' => true, and
'same_site' => 'lax'(setting to "strict" did not seem to work)
in my config/session.php file.
Now it is back to testing security and tweaking things back if need be.

Laravel post route returning error 500

my route
Route::post('register', function()
{
return "POST SUCCESS!";
});
works fine if I chane it to "get".
As soon as I change it to post (and use postman to actually send a post request) I get an error 500.
Its driving me crazy and I cant find the answer..
There is also nothing in the php error log... o_0
Make sure you have created .env file for local environment.
Take a look at storage/logs/laravel.log, you should find error there, e.g.:
cat storage/logs/laravel.log | grep "\[201

Resources