2

I'm using Yii 1, and testing an API i'm working on, and when i POST to this URL

https://blabla.com/products/report/notification

I get nothing on postman

My Filters

public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

My action is

public function actionnotification() {

        print_r($_POST);
        // print_r(Yii::app()->request->getPost('report_id')); //this didn't work either
        die;
}

the URL is correct, and if i echo 'hi'; in the action above, it does echo hi.

and my rules are

public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('notification'),
                'users'=>array('*'),
            );
     }

Here is my exported cURL from postman

curl -X POST \
  https://blabla.com/products/report/notification \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Cookie: PHPSESSID=puevhejh61fg4f0qtc52nbg3jm' \
  -H 'Postman-Token: 4fb0526b-f26e-4802-bf13-8361f65a26af,b610902b-25f1-4eb9-9ce6-cfddec164788' \
  -H 'Referer: https://blabla.com/products/report/notification' \
  -H 'User-Agent: PostmanRuntime/7.18.0' \
  -H 'cache-control: no-cache' \
  -d 'report_id=123131231&status_id=1'

When i change it to a params and GET it works, but POST, body x-www-form-urlencoded and a GET, body x-www-form-urlencoded doesn't work, is there something I'm missing here? Thank you

Shaho
  • 157
  • 3
  • 15

1 Answers1

0

Try sending the POST data in the body instead. I usually use Yii methods instead of $_POST or $_GET: https://www.yiiframework.com/doc/guide/2.0/en/runtime-requests

Edit: Here's my postman configuration for sending POST requests postman

Edit2: In your config file, you should add to components/requests a property named enableCsrfValidation and set it to false. I'll leave you mine:

'components' => [
    'request' => [
        'cookieValidationKey' => {key},
        'enableCsrfValidation' => false
    ],
...
  • i tried both `x-www-form-urlencoded` and `form-data`, still blank – Shaho Aug 05 '20 at 12:44
  • I know it's a no-brainer, but are you changing the request method from GET to POST? (it happened to me before) like this https://imgur.com/rvbMlty – user3502640 Aug 05 '20 at 13:20
  • I've started a fresh project to test your issue, but I'm not being able to reproduce. Can you dump the $_REQUEST instead? I would like to see what's reaching the serverside. Also, do you have CSRF validation disabled in your project? The only way I'm getting an empty array as response, is when I'm not sending the POST data. – user3502640 Aug 05 '20 at 21:49
  • Does csrf need to be off for an API? If so how do I turn it off just for the api or how do I keep it on? – Shaho Aug 06 '20 at 06:11
  • I had to deactivate it in order to be able to send POST requests through POSTMAN. I just edited my original answer with an example. – user3502640 Aug 06 '20 at 12:17