0

Here i write APIfor register schedule for that using POSTmethod,for checking API using postman but it not work with POST method if i using GET method insted POST it workin?

 public function actionRegister() {
        $response = [];

        if (isset($_POST['practiceCode']) &&
            isset($_POST['officeID']) &&
            isset($_POST['token']) &&
            isset($_POST['workingDays']) &&
            isset($_POST['morningStart']) &&
            isset($_POST['morningEnd']) &&
            isset($_POST['afternoonStart']) &&
            isset($_POST['afternoonEnd']) &&
            isset($_POST['eveningStart']) &&
            isset($_POST['eveningEnd'])) {

            $practiceModel = Practice::find()->where(['practiceCode' => $_POST['practiceCode'], 'deleted' => 'N'])->one();
            if ($practiceModel != null) {
                $officeModel = Office::find()->where(['practiceCode' => $_POST['practiceCode'], 'deleted' => 'N'])->all();

                if ($officeModel != null) {
                    $arrayOffice = Office::find()->where(['id' => $_POST['officeID'], 'deleted' => 'N'])->one();

                    if($arrayOffice != null) {
                        $scheduleModel = new Schedule();
                        $scheduleModel->officeID = $_POST['officeID'];
                        $scheduleModel->token = $_POST['token'];
                        $scheduleModel->workingDays = $_POST['workingDays'];
                        $scheduleModel->morningStart = $_POST['morningStart'];
                        $scheduleModel->morningEnd = $_POST['morningEnd'];
                        $scheduleModel->afternoonStart = $_POST['afternoonEnd'];
                        $scheduleModel->eveningStart = $_POST['eveningStart'];
                        $scheduleModel->eveningEnd = $_POST['eveningEnd'];
                       // $scheduleModel->practiceCode = $_POST['practiceCode'];
                        if ($scheduleModel->save()) {
                            $response = ['code' => '200',
                                'result' => 'Success',
                                'message' => 'Schedule created successfully',
                                'details' => $scheduleModel->toArray(),
                            ];
                        } else {
                            $response = ['code' => '400',
                                'result' => 'Failure',
                                'message' => 'Could not Schedule you at this moment'];
                        }
                    } else {
                        $response = ['code' => '400',
                            'result' => 'Failure',
                            'message' => 'Specified Office does not exist.',
                        ];
                    }
                } else {
                    $response = ['code' => '400',
                        'result' => 'Failure',
                        'message' => 'Specified practice code Office does not exist.',
                    ];
                }
            } else {
                $response = ['code' => '400',
                    'result' => 'Failure',
                    'message' => 'Specified practice code does not exist. ',
                ];
            }
    } else {
            $response = ['code' => '400',
                'result' => 'Failure',
                'message' => 'Invalid request/missing parameters',
            ];
        }

        \Yii::$app->response->format = 'json';
        return $response;
    }

2 Answers 2

2

For security purpose, yii 2.0 doesn't allow third party POST request. For example, in your case Postman. To enable your POST request, add this in your controller:

this->$enableCsrfValidation = false;

You can check stackoverflow site, there are plenty of topics related to this.Check out:
Disable CSRF validation for individual actions in Yii2

For further reading check out:
Yii 2.0 reference

Sign up to request clarification or add additional context in comments.

1 Comment

I have already add this " public $enableCsrfValidation = false;" in my Controller
0

app/config/web.php

You need to add 'application/json' => 'yii\web\JsonParser' into request array.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.