tl;dr How can I modify JSON output of GET
request of my API application so that anything user puts into one of the model’s fields (textbox; presumably JSON) is output as a JSON, not as a string?
I have a very simple RESTapi application (based on Yii 2 Advanced Project Template). It is simple enough so I can use basic controller setup of:
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
I don’t have any extra actions and therefore I do not modify output in anyway. I have just configured my application to output JSON, but I let all the dirty work with output generation to be done by Yii.
My current setup generates responses like that:
{
"id": 1,
"to": 2,
"from": 1,
"type": 1,
"status": 3,
"body": "",
"result": "",
"created_at": 1698085035,
"updated_at": 1698085315
}
User can enter anything (but presumably JSON) into body
fields and its gets output like that:
{
"id": 5,
"to": 2,
"from": 1,
"type": 0,
"status": 1,
"body": "{\r\n \"operation\": \"move\",\r\n \"engine\": \"left-1\",\r\n \"speed\": 5,\r\n \"timeframe\": 4,\r\n \"direction\": 1\r\n}",
"result": "",
"created_at": 1698085399,
"updated_at": 1698221734
}
What steps must I undertake to have the body
field formatted / output like it would be just another part of the main JSON code returned by this GET
request?
For me it doesn’t matter, if for body
JSON is formatted at the output only or if user-data provided to textbox gets converted and stored as JSON in DB. All that matters is to get pure / clean JSON instead of above-exampled string-like “garbage”.
The model behind is in relation to another model, so if I use expand
field in request, I am getting a beautifully merged data, all in JSON:
{
"id": 1,
"to": 2,
"from": 1,
"type": 1,
"status": 3,
"body": "",
"result": "",
"created_at": 1698085035,
"updated_at": 1698085315,
"destination": {
"id": 2,
"type": 1,
"status": 1,
"tid": "T1A-1",
"name": "TC-MED Platform",
"created_at": 1698079476,
"updated_at": 1698079476
}
}
But it seems I can’t achieve the same for user-formatted data.
BTW: This question is to only know the methodology that I must implement. I know and understand that I have to utilize any kind of checking and purification of the user-provided input. But that’s not a case in this question.