Reputation: 41
I am making rest API with Yii Plus when I am trying to print_r request (using Postman) it's empty, can anyone let me know what I am doing wrong.
<?php
namespace frontend\controllers;
use Yii;
use yii\rest\Controller;
class ApiController extends Controller
{
Const APPLICATION_ID = 'ASCCPE';
private $format = 'json';
public function actionUserRegister()
{
$request = \Yii::$app->request->post(); $post = (file_get_contents("php://input"));
print_r($request);
die('sdw');
}
}
Upvotes: 1
Views: 844
Reputation: 6169
You are not trying to print request. You are trying to print post data, but you are not sending any post data by your request.
The \Yii::$app->request->post();
returns data from $_POST array. This array is filled from request body only for data that have been sent in form-data
or x-www-form-urlencoded
format.
In postman click open the body part of request, select one of the two mentioned formats and fill the data you want to send.
If you want to use other format for request, like json or xml, you have to read it from php://input
. You already have it in your code:
$post = (file_get_contents("php://input"));
So try to print the $post
instead of $request
variable. But you still need to fill the body part of request in postman.
The params you've set in postman are the GET params. Those are part of request's url. You can get them for example like this:
$request = \Yii::$app->request->get();
Upvotes: 1
Reputation: 883
You are returning with the message in the die function.
Instead of this you can try this way:
die(print_r($request, true));
Example:
public function actionCreate()
{
$request = Yii::$app->request->post();
die(print_r($request, true));
}
better:
return print_r($request, true);
Example:
public function actionCreate()
{
$request = Yii::$app->request->post();
return print_r($request, true);
}
better:
// include the VarDumper class
\Yii::info(VarDumper::dumpAsString($request));
// output will be located in your app.log file
More info on the print_r function
Example:
public function actionCreate()
{
return Yii::$app->request->getBodyParams();
}
Upvotes: 0