Answer the question
In order to leave comments, you need to log in
How to fix 400 error when sending POST request?
Good day!
For the past few days, I've been facing a problem when testing the backend API on Yii2 and the very basis from Postman algorithms:
When entering a POST request, Yii2 itself accesses the controller and its action, to whom I directly access and it should only display text and nothing else:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\View;
...
use yii\web\NotFoundHttpException;
class SiteController extends Controller{
...
public function actionServiceCodeCenter($service){
Yii::$app->controller->enableCsrfValidation = false;
$q = json_decode($_POST['serviceQuery']);
switch($service){
...
if($_POST['serviceQuery']){
$source = $q['fsq'];
$generateCode = [
ceil(getRandomFromRange(1000,9999)),
ceil(getRandomFromRange(2000,4600))
];
$isSignUp = $source['svc'] === 'SignUp' ? TRUE : FALSE;
$isForgot = $source['svc'] === 'Forgot' ? TRUE : FALSE;
if($isSignUp){ $newCode = $generateCode[0]; }
else if($isForgot){ $newCode = $generateCode[1]; }
throw new HttpException(201 ,$newCode);
}
else{ throw new HttpException(405 ,'Query conflict'); }
break;
default: throw new HttpException(404 ,'Service not found'); break;
}
}
}
?>
Answer the question
In order to leave comments, you need to log in
use yii\web\Controller;
->
yii\rest\Controller
and read about REST controllers.
Or about disabling csrf in web controllers (bad).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question