Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process Vue/Axios Json payload posted data on Yii2

It took me a while to understand this, being that it was a little obvious. I will answer myself, so other can benefit of the answer and ofcourse to see if there's a better way to do this. The problem was based on Axios/Yii2 but I guess this will apply equally to other frontend libraries/frameworks sending data to Yii2.

I needed to post data from a small form made on Vuejs, sending the request Axios to a Action/Controller on Yii2, so data is sent on a simple POST request and the post is getting to the controller, but I was not able to receive the data on the action, $_POST | $post arrives empty (checked using xdebug).

As much as I remember, this had something to do with security. But I already tried by disabling public $enableCsrfValidation, so that was not the problem.

public $enableCsrfValidation = false;

But no matter what, data was not being added to the request/post data inside Yii2.

The following Image, explains the problem you will find there:

  1. The Axisos method that sends the post with test data.
  2. The Yii2 Action stpoed at the place, I should be able to see data.
  3. The capture of the xdebug variables and data for the request.
  4. The capture of Chrome where you can check the payload is sent.

enter image description here

like image 468
Pablo Palacios Avatar asked Dec 24 '22 06:12

Pablo Palacios


1 Answers

The answer is as I said "kind of obvious", but I could not see that, and I am sure some other devs will probably fall on this.

After searching like crazy and asking everyone, I tried sending the request by using Postman app, yup the best thing I know to test apis.

enter image description here

Dont forgue to add the xdebug cookie to be able to debug your PHP Endpoint.

enter image description here

There I found the first clue «the obvious part», I was not sending data as a form-data, Axios and other libraries, send the data as a raw (application/json) payload.

This means that Yii2 will no be able to find the data inside the post request, yes its there but Yii2 magic will not work, neither you will find this data inside $GLOBALS or in $_POST.

So reading the Yii2 documentation I found that inside request I can use a function that will help me recovering the Raw data, so to do this use the following line:

$raw_data = Yii::$app->request->getRawBody();

Now, that data gets to you as a simple, raw json string, so use the power of PHP to parse it to an object.

$object= json_decode($raw_data );

And finally use the data inside by calling the properties you look for, sent on the pay load:

Json Payload:

{
    "msg":"This is my payload",
    "id":"11"
}

To use it:

echo $object->{'msg'}; // prints: This is my payload

So that's the way to handle that, now I would like some other points of view to see if there's a better way or cleaner way to do this. Hope it helps.

like image 57
Pablo Palacios Avatar answered Jan 13 '23 15:01

Pablo Palacios