Laravel 5.4 Route::post() NotFoundException - php

Hi I have a problem with Laravel 5.4 routes, Route::get() are working but Route::post() give me NotFoundException error back.
Take a look at login route in api.php this one is for sure not working but was working before I started using Homestead and vagrant.
Any ideas ? Here are my interresting configurations about API and routes (Hosted files on github) :
1) RouteServiceProvider.php
2) api.php
3) Kernel.php
Can't understand why ! If needed I can upload full project but I can't stand why it doesn't work since start of the week.
Please be kind with me this is my first project with Laravel.
Kind regards
Edit : Just added php artisan route:list --path:foobar result :
route-list-ouptut

Your code seems fine, except I would remove these lines from api.php:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
As I understand these are request headers. So you should add the in your Postman request headers (or another Rest client). And not in any where in your code.
If you want to add headers to response, rather then request here are Laravel Create Response Docs

Related

All requests ends up in GET route. Laravel

I have a Laravel(8.83) application. API routes end up in GET requests always.
To check the problem I have deleted all the routes except one. Now I have just one route:
Route::put("cart/{id}", function(Request $request, $id) {
return $request->all();
});
Although I make PUT request on the postman, the result is: "The GET method is not supported for this route. Supported methods: PUT."
I have tried PUT, PATCH, POST, DELETE, and OPTIONS. All ended up in the same error.
Edit: 3 Oct 2022
I run php artisan optimize:clear and composer dump-autoload several times. None resolved the issue.
The project is in production, deployed on a server. I have made a request from a Vue.JS web app client, and the result is the same. I don't think the problem is caused by Postman.
The request method is probably getting modified in Postman or in webserver config, or in .htaccess.
Here's one thread on a similar issue in Postman - Postman support
For a reference to a working .htaccess - Laravel .htaccess GitHub

CORS issue on same domain Ajax/PHP

I'm facing a CORS problem with an ajax call.
I have a php file called through ajax with the following headers
header('Access-Control-Allow-Origin: * ');
header('Access-Control-Allow-Methods: GET, ADD, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: *'); // also tested with Content-Type, x-requested-with
There is no error when the file is called from
http://www.mywebsite.com
but it throws a 403 forbidden error when it's called from
https://mywebsite.com
https://www.mywebsite.com
To be more specific the GET is ok, but the ADD is not allowed.
As we're talking about the same domain, i don't understand why there's a '403' error
Thanks for your help,
what php framework
if this is laravel you are using make sure you see the file config/cors.php
or the file VerifyCsrfToken
protected $except = [
'*'
];

Cakephp 4: JWT Authorization fails in Browser, Postman works

I'm having trouble migrating my Backend CakePHP App, from CakePHP 3.9 to CakePHP 4.
The App serves as an API for my sveltejs/sapper Javascript Application.
As for the JWT Authentication I'm using the admad/cakephp-jwt-auth plugin.
The App before Migration works perfectly from the browser as the frontend.
Testing my API in Postman works for both CakePHP 3 and 4 versions.
After migration however, requests sent from the browser (sveltejs/sapper) and where Authentication is needed (Authorization header containing the Bearer token) result in an error:
Error 401 "Authorization failed
My CORS Middleware (ozee31/cakephp-cors plugin) also seems to be working since requests that don't need to be authorized just work as expected, also from the browser - unplugging CORS middleware result in an CORS error in the browser for those requests.
Also, my server is populating $_SERVER['HTTP_AUTHORIZATION']
Has anyone successfully implemented a similar setup, especially when it comes to CakePHP 4 and HTTP_AUTHORIZATION header yet?
I created a own JWT plugin for CakePHP 4 that worked as a component.
https://github.com/WireCore/CakePHP_JWT
It's currently in alpha but you can try if it solved your problem.
You can install it with composer composer require wirecore/cakephp_jwt and add it in your Application.php via $this->addPlugin('Wirecore/CakePHP_JWT'); You find more details in the README
Cors error can be fixed in following way .
In AppController.php
private function setCorsHeaders()
{
$this->response = $this->response->cors($this->request)
->allowOrigin(['*'])
->allowMethods(['*'])
->allowHeaders(['x-xsrf-token', 'Origin', 'Content-Type', 'X-Auth-Token', 'authorization'])
->allowCredentials(['true'])
->exposeHeaders(['Link'])
->maxAge(300)
->build();
}
public function beforeRender(EventInterface $event)
{
// .......
$this->setCorsHeaders();
}
public function beforeFilter(EventInterface $event)
{
// ......
if ($this->request->is('OPTIONS')) {
$this->setCorsHeaders();
return $this->response;
}
}
In config/Bootstrap.php add this line al last line og file.
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");

Preflight response is not successful with proper headers

I'm having issue getting my ionic app to POST to my API. On my api I have set the following headers:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS");
When posting from Postman or the actual website, everything functions as expected and I see these headers come back but once I open up my app and send a request, it no longer works.
GET Requests are working fine, it's just POST requests that are broken. I use the following to send a post request on my app:
/**
* Post to the API
* #param path Where to go
* #param params What to send
*/
private post(path, params): Promise<any> {
return this.http
.post(this.apiUrl + path, params)
.toPromise()
.then(r => r.json());
}
I get the following error inside of my ionic app
Failed to load resource: Preflight response is not successful
XMLHttpRequest cannot load https://mmcalc.com/api/calculate. Preflight response is not successful
I've been pulling my hair out over this for nearly 15 hours now, I don't understand why it won't work.
This issue happened due to the WKWebView Mostly
So, the very simple way to use the WKWebView in Ionic is that You Must uninstall the previously installed UIWebView from the plugin by running the following command.
ionic cordova plugin remove cordova-plugin-ionic-webview
After that try to add the WKWebView Plugin with this command
ionic cordova plugin add cordova-plugin-wkwebview-engine
When the WkWebView plugin is installed the very next thing is to add the following lines in the config.xml file
feature name="CDVWKWebViewEngine"
param name="ios-package" value="CDVWKWebViewEngine"
feature
preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine"
preference name="WKWebViewOnly" value="true"
After doing all that when you try to run the application and hit some api call you will get the preflight issue in that due to CORS so to fix that. Simply run the following command
ionic cordova plugin add cordova-plugin-wkwebviewxhrfix
After adding the above plugin the CORS issue will be resolved
Thanks,
Happy Coding
If you're only having the issue in iOS then it sounds like an issue with WKWebView
https://ionicframework.com/docs/wkwebview/
When the server receives the OPTIONS request, what does it respond with?
I would make sure the response from the OPTIONS request contains the headers WKWEBView is expecting, otherwise it will prevent the code from the making the call.
The solution was very simple but not very obvious, I simply had to set the proper headers on my API. This article solved the issue for me and was very simple.
# Always set these headers.
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
the preflight request uses the OPTIONS request method. You should make sure that your API endpoint returns a successful status code for the OPTIONS request method and the response should also include the CORS headers which you have mentioned above. GET requests are probably working because they dont need a prefligh request. I would guess that in your case the OPTIONS method returns a wrong status code.

IONIC Content-Type is not allowed by Access-Control-Allow-Headers

I was working on an angularJS, IONIC project with CI backend.
I was sending parameters to the server using POST Method, and receiving it using the CodeIgniter Input class and everything was working OK.
Now that app going to be native Android, the developer should send parameters as JSON array. So I changed my backend API function to receive it like:
$data = json_decode(file_get_contents('php://input'), true);
$email = $data->email;
It works great but the problem now is when I try to access the function using IONIC app I get an php error.
XMLHttpRequest cannot load
http://192.168.1.122/project/index.php/api/login.
Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
on my API I allowed all CORS
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
...But I still get the same error.
Can I get any other solution around this point.
Thanks for your help.
You have to add cordova-plugin-whitelist plugin to implements a whitelist policy for navigating the application webview on Cordova 4.0
ionic plugin add cordova-plugin-whitelist
then
ionic build <platform>
Source

Categories