Good I have a problem that has me desperate, to see if someone can lend me a hand, the situation is as follows.
I have a REST API in Symfony 3, which I use with an Angular front, the application as a whole works perfectly in an Apache server that I have in local, but when uploading it to a free server that I have in Hostinger, only the requests work for me GET, the POST give me the following error
GET "ruta" 405 (Method Not Allowed)
From what I've been seeing everything points to a CORS error, I think it's worth mentioning that I get the same result from Postman, the customer Restfull from chrome, so I ruled out that it's an error from angular requests, focusing on the configuration of the backend, I have the following:
In the frontcontroler, I have been using the following code for the first time:
header ("Access-Control-Allow-Origin: *");
header ("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With,
Content-Type, Accept, Access-Control-Request-Method");
header ("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header ("Allow: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER["REQUEST_METHOD"];
if ($method == "OPTIONS"){
die ();
}
As I gave the error mentioned and after searching the internet I added the following file .htacces:
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"
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
As the error persisted I opted to add the following code in the specific Symfony driver that I am accessing when I get the error:
$respuesta = new JsonResponse($registro);
$response->setContent($respuesta);
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
Still not working, I do not know what else to try, I'm starting to suspect that Hostinger has limited this in some way, but I do not know how, or if this is possible, what I do know is that the .htaccess files (in general) are enabled because I have others working in other folders.
If it occurs to someone that it may be failing, I will appreciate any input.
Edito: If you want to see the application in operation, it is in link to see the error "access" above to the right and try to login.
driver code:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\JsonResponse;
class DefaultController extends Controller{
public function indexAction(Request $request){
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),]);
}
public function loginAction (Request $request){
$helpers = $this->get("app.helpers");
$jwt_auth = $this->get("app.jwt_auth");
$json = $request->get("json", null);
if ($json != null){
$params = json_decode($json);
$email = (isset ($params->email)) ? $params->email : null;
$password = (isset ($params->password)) ? $params->password : null;
$getHash = (isset ($params->getHash)) ? $params->getHash : null;
$emailConstrain = new Assert\Email();
$emailConstrain->message = "El email no es valido";
$validate_email = $this->get("validator")->validate ($email, $emailConstrain);
//cifrar password
$pwd = hash ("sha256", $password);
if (count ($validate_email) == 0 && $password != null){
if ($getHash == NULL || $getHash == "false"){
$signup = $jwt_auth->signup($email, $pwd);
}else{
$signup = $jwt_auth->signup($email, $pwd, true);
}
return new JsonResponse($signup);
//echo "Datos conseguidos!!";
}else{
return $helpers->json(array(
"status" => "error",
"data" => "Login Incorrecto!!"
));
}
}else{
return $helpers->json(array(
"status" => "error",
"data" => "Envia JSON por POST!!"
));
}
}
}
I also put the yml file corresponding to the routes per if it was what you wanted to see:
default_index:
path: /
defaults: { _controller: "AppBundle:Default:index"}
methods: GET
default_login:
path: /login
defaults: { _controller: "AppBundle:Default:login"}
methods: POST
Angular request code:
signup(user_to_login) {
let json = JSON.stringify (user_to_login);
let params = "json="+json;
let headers = new Headers({'Content-Type':'application/x-www-form-urlencoded', 'Accept': 'application/x-www-form-urlencoded'});
let options = new RequestOptions({ headers: headers });
return this._http.post (this.url+'/login', params, options)
.map(res=> res.json());
}