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;
    }
ArK
  • 20,698
  • 67
  • 109
  • 136
Priyanka Ahire
  • 432
  • 2
  • 9
  • 28

2 Answers2

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

Community
  • 1
  • 1
Tajmin
  • 353
  • 1
  • 7
  • 23
0

app/config/web.php

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

enter image description here

karel
  • 5,489
  • 46
  • 45
  • 50
Alan Tavares
  • 51
  • 1
  • 1