trying to setup yii2 as a api backend for a react site contact form frontend using cors,…
- User
trying to setup yii2 as a api backend for a react site contact form frontend using cors, csrf token in yii2.. but when i try and pass the form from react back to yii2 i get a 2025-03-13 01:18:26 [::1][-][-][error][yii\web\HttpException:400] yii\web\BadRequestHttpException: Unable to verify your data submission.
- Assistant
- User
if i use this:
'response' => [ 'format' => yii\web\Response::FORMAT_JSON, 'formatters' => [ yii\web\Response::FORMAT_JSON => [ 'class' => 'yii\web\JsonResponseFormatter', 'prettyPrint' => YII_DEBUG,// Use "pretty" formatting in debug mode 'encodingOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, ], ], ],do i have to use this in my controller?
public function actionSubmitForm(): Array { \Yii::$app->response->format = Response::FORMAT_JSON;??
- Assistant
No, you don't necessarily need to set the response format to
Response::FORMAT_JSONin every action of your controller if you have already configured the global response format in your application configuration (i.e., in yourconfig/web.php).When you set the
'response'component's format toyii\web\Response::FORMAT_JSON, all of your controller actions will default to returning JSON responses unless overridden within the action itself.When to Set the Response Format
- User
An Error occurred while handling another error: yii\base\UnknownPropertyException: Setting unknown property: yii\web\JsonResponseFormatter::encodingOptions
- Assistant
The error message you are encountering,
yii\base\UnknownPropertyException: Setting unknown property: yii\web\JsonResponseFormatter::encodingOptions, indicates that you are trying to set a property (encodingOptions) on the class that does not exist. - User
what about using session with csrf?
- Assistant
- User
- Assistant
- User
'HTTP_X_CSRF_TOKEN' => 'Gl9curoN3pOi5tEzByQaAOPJo-YSbAc177sDPcfec0BIKCXI8z64y_rUlWJAYHJyj5Phsn4pVUKejWVopK0JNQ==' ??? I thought it was X-CSRF-Token
- Assistant
- User
i get the error: Invalid CSRF Token
- Assistant
- User
$behaviors['corsFilter'] = [ 'class' => Cors::class, 'cors' => [ 'Origin' => ['http://localhost:3000'],// Adjust based on your needs //'Origin' => ['http://www.myserver.com', 'https://www.myserver.com'], 'Access-Control-Request-Method' => ['GET', 'POST', 'OPTIONS'], 'Access-Control-Request-Headers' => ['X-CSRF-Token', 'Content-Type'], // Allow credentials (cookies, authorization headers, etc.) to be exposed to the browser 'Access-Control-Allow-Credentials' => true, // Allow OPTIONS caching 'Access-Control-Max-Age' => 3600, ], ];
return $behaviors; - Assistant
- User
do i need to set anything within the web.php for session?
- Assistant
- User
2025-03-13 13:36:35 [::1][-][-][error][app\controllers\ApiController::actionSubmitForm] CSRF Token from headers: "iMF_19y77WzKmPhSa6slNc9MuOIqZPgyL6mIas_eaGPJhBmQpPivCJPBzict2mB8nBrSpm4QkWdeyMcZoKsvCA==" in /www/yii2-agency/controllers/ApiController.php:115 2025-03-13 13:36:35 [::1][-][-][error][app\controllers\ApiController::actionSubmitForm] Invalid CSRF token: "iMF_19y77WzKmPhSa6slNc9MuOIqZPgyL6mIas_eaGPJhBmQpPivCJPBzict2mB8nBrSpm4QkWdeyMcZoKsvCA==" in /www/yii2-agency/controllers/ApiController.php:121 2025-03-13 13:36:35 [::1][-][-][info][application] $_GET = []
$_POST = []
$_FILES = []
$_COOKIE = []
$_SERVER = [ 'DOCUMENT_ROOT' => '/www/yii2-agency/web' 'REMOTE_ADDR' => '::1' 'REMOTE_PORT' => '34912' 'SERVER_SOFTWARE' => 'PHP 8.2.26 Development Server' 'SERVER_PROTOCOL' => 'HTTP/1.1' 'SERVER_NAME' => 'localhost' 'SERVER_PORT' => '8080' 'REQUEST_URI' => '/api/submit-form' 'REQUEST_METHOD' => 'POST' 'SCRIPT_NAME' => '/index.php' 'SCRIPT_FILENAME' => '/www/yii2-agency/web/index.php' 'PATH_INFO' => '/api/submit-form' 'PHP_SELF' => '/index.php/api/submit-form' 'HTTP_HOST' => 'localhost:8080' 'HTTP_USER_AGENT' => 'Mozilla/5.0' 'HTTP_ACCEPT' => 'application/json, text/plain, ' 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5' 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, br, zstd' 'CONTENT_TYPE' => 'application/json' 'HTTP_CONTENT_TYPE' => 'application/json' 'HTTP_X_CSRF_TOKEN' => 'iMF_19y77WzKmPhSa6slNc9MuOIqZPgyL6mIas_eaGPJhBmQpPivCJPBzict2mB8nBrSpm4QkWdeyMcZoKsvCA==' 'CONTENT_LENGTH' => '55' 'HTTP_CONTENT_LENGTH' => '55' 'HTTP_ORIGIN' => 'http://localhost:3000' 'HTTP_CONNECTION' => 'keep-alive' 'HTTP_REFERER' => 'http://localhost:3000/' 'HTTP_SEC_FETCH_DEST' => 'empty' 'HTTP_SEC_FETCH_MODE' => 'cors' 'HTTP_SEC_FETCH_SITE' => 'same-site' 'HTTP_DNT' => '1' 'HTTP_SEC_GPC' => '1' 'HTTP_PRIORITY' => 'u=0' 'REQUEST_TIME_FLOAT' => 1741872995.7431 'REQUEST_TIME' => 1741872995 ]
- Assistant
- User
if im looking for 'X_CSRF_TOKEN' but the return is being changed to 'HTTP_X_CSRF_TOKEN'
would this not be a issue?
- Assistant
- User
- Assistant
- User
so eveytime we grab the token in the contact.js we need to first clear the cookie?
- Assistant
- User
WORKED!!!!
- Assistant
That's fantastic to hear! 🎉 I'm glad that the adjustments worked and you were able to resolve the CSRF token issue in your Yii2 and React application.
If you have any more questions or need further assistance with Yii2, React, or anything else, feel free to ask. Good luck with the rest of your project!