MethodNotAllowedHttpException with lucadegasperi/oauth2-server-laravel's included AccessTokenFlow POST route - laravel

This is my first Laravel project I'm on, and it has been a ton of fun so far.
I'm setting up an OAuth2 server. I have copied the code posted here in to my routes file.
Via this block of code...
Route::post('oauth/access_token', function()
{
return AuthorizationServer::performAccessTokenFlow();
});
I have tried doing http://local.server.com/oauth/access_token and a "MethodNotAllowedHttpException" error.
If there is any other information I could provide that would help you help me, please tell me!
Cheers

If you are typing http://local.server.com/oauth/access_token into the browser URL bar, then you are sending the request:
GET oauth/access_token
However, your route handles a POST request, and since there is no GET route defined, Laravel is responding with MethodNotAllowedHttpException
In order to properly test your route, you will need to send a POST request.

Related

CakePHP: can't access the Session when making AJAX call

This question is for CakePHP 4.3:
In my action, I am accessing the session. For a normal GET request, everything works fine. If I call the same action through an AJAX request, I do not have access to the session. Why is that?
For example, even this does not work:
public function select3() {
debug($this->request->getSession()->read());
}
For a GET request, the session is printed. For an AJAX call, an empty array is printed.
Is the AppController NOT called for an AJAX request?
Any help is appreciated!
First, thanks to "ndm" for your offered help.
I solved it after seeing that something was mixed up with the URLS.
The URL has "server-4.2" in it, and "server" is a link to it.
Both "server-4.2" and "server" seemed to have confused the Authentication controller.
Glad it works now.

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.

MethodNotAllowedHttpException when using Route::delete

I don't know why I get this
I tested the route with
Route::delete("/attachments/{url}","TrixAttachmentController#destroyAttachment");
localhost:3000/api/v1/attachments?url=hDXtilCleTWc6WyiMeWpp9O0xIx3cRyJuEwVPxzL.jpeg
public function destroyAttachment($url){
dd($url);
}
still i get it
localhost:3000/api/v1/attachments?url=hDXtilCleTWc6WyiMeWpp9O0xIx3cRyJuEwVPxzL.jpeg
here ?url it shows it now using get request
call this route via delete method
can you show form
When using the Route::delete, the request is expecting a POST request with a method of DELETE.
If you are hitting the route in the browser using a GET request, this is the sort of error that will be thrown.
If you are calling the route in a browser you need to change that.
Make sure you are calling it on a form using DELETE verb.

Vuejs Axios POST request gets HTTP422 error from Laravel backend

Hi I am using Vuejs in the frontend and Laravel in the backend. The role of Laravel is handling the API only. The frontend and backend are separated, i.e. I am not using Vuejs in Laravel's resource/js folder.
Now I am sending Axios POST request from Vuejs to Laravel. All the form input values are prevalidated using HTML5 required attribute. And when I console.log the request data, it shows all the fields filled.
In Vue file:
const data = {
name: this.name,
gender: this.gender,
mobile_no: this.mobile_no,
image: this.userImage
};
console.log("Request data . . . .", data);
const response = await this.axios
.post(`${this.AppURL}/admin/user/create`, data, {
headers: {
"Content-Type": "multipart/form-data"
}
})
.then(() => {
console.log("Success. . . . ")
alert("Successfully Driver Added");
})
.catch(error => console.log(error));
And in Laravel, the request is passed through some validation. It's a simple validation to check if all the fields are filled.
I am also using JWTAuth package for the authentication, and the token is generated by it.
It's too much code to write them all the way down here. But I am sure you can understand what I mean.
What I am getting as a response is this
POST http://localhost:8000/api/admin/user/create 422 (Unprocessable Entity)
The actual result I am expected to get is either success or some errors that is according to some if conditions in validation or token check.
I tried to figure out where this error might come from. What I think at the moment is this could be due to the absence of csrf_token in the POST request. As I'm sending the request outside Laravel, csrf_token is missing in the form. I am not 100% sure though about this.
So my question is:
How can I include csrf_token in Axios POST request, when I send it from outside Laravel.
If this 422 error is not related with csrf_token, what could be causing this? Any previos experiences like min? and any solutions for this?
Thanks in advance for your help.
Please, modified catch block as #Jack suggested:
.catch(error => {
console.log("ERRRR:: ",error.response.data);
});
Now you can get errors and handle errors in the catch block.
.catch(function (error) {
console.log(error.response.data.errors);
});
please use this code I hope it work's.
I was also facing the same issue, i think it is due to some Headers missing in your Api request from vue.js. here some tips which may helps you to solve this issues.
Make sure that you are protecting your Api Routes or not(by sanctum or something else). If you are protecting , then make make sure that you are sending authentications token in headers.
Second make sure that your request(axios or jwt) should contained valid data, if your are sending images or files etc make sure how can we send them.
First, get request and check in laravel by dd($erquest->all()); if you are geeting data then validate, it is possible that laravel request doesnt contained your sending data..
These errors may be caused due to follow reasons, ensure the following steps are followed.
To connect the local host with the local virtual machine(host).
Here, I'am connecting http://localhost:3001/ to the http://abc.test
Steps to be followed:
We have to allow CORS, placing Access-Control-Allow-Origin:* in header of request may not work. Install a google extension which enables a CORS request.
Make sure the credentials you provide in the request are valid.
Make sure the vagrant has been provisioned. Try vagrant up --provision
this make the localhost connect to db of the homestead.
Just click on the preview tab within network section in the dev tool, you are going to see the actual error message.

Laravel 5 -- Redirect in Controller is not working in AJAX call

Important: this comes from ajax call.
Everything works perfectly except:
use Illuminate\Routing\Redirector;
public function my_call() {
return redirect()->route('page-1');
}
Throws 500 error.
return view('page-1') works without problems.
Maybe anybody sees what I am doing wrong?
Thank you.
Do you have an actual route with the name that you are using, you cannot redirect to a view as these are returned by routes
Apperantly you cannot do this on server side if you do AJAX call:/
Here an explanation: https://laracasts.com/discuss/channels/vue/redirect-after-ajax-post-request
It's for vue but I believe it applies universally for ajax.

Resources