Yii Framework Forum: Sending JSON from the client. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Sending JSON from the client.

#1 User is offline   nayti97 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 23-August 17

Posted 23 August 2017 - 04:56 PM

Hi. I used REST API in my project.

Created table elements(id, content)

Created Module

Element

namespace app\models;
use yii\db\ActiveRecord;



class Element extends ActiveRecord{
    
    
    
    public static function tableName(){
        return 'elements';
    }
    
    
    public function rules(){
        return [

            [['content'], 'required'],
            
        ];
    }
    
    public function fields()
{
    return ['id', 'content'];
}
  
    
    


Creatd Controller

ElementController

namespace app\controllers;
use yii\rest\ActiveController;
use app\models\Element;
use yii\rest\Controller;


class ElementController extends ActiveController{
    
    public $modelClass = 'app\models\Element';
    
    public function actions()
{
    $actions = parent::actions();
    return $actions;
}
    
}


On client create curl

$url = "api.test.loc/elements";
$request_data = ['content' => 'dddd'];
$json = json_encode($request_data);
print_r($json);
echo '<br>'; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


$response = curl_exec($ch);
curl_close($ch);
print_r($response);


But my api sends me an error:

[{"field":"content","message":"Content cannot be blank."}]


Help me. How to write a json correctly on client???


Sorry for the grammar - I do not know much English
0

#2 User is offline   toaster 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 24-December 13
  • Location:Perugia - Italy

Posted 08 September 2017 - 09:15 AM

Mmm I would add a validation rule for the content attribute, something like:
public function rules(){
        return [
            ['content', 'string'],
            [['content'], 'required'],            
        ];
    }
choosing a validator from Here.
Moreover I would add also:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
Another thing you might do is check whether the body request has correctly embedded the json array. Unfortunately REST is stateless and do not holds any session so the Yii2 debug toolbar or XDebug won't work. For properly debug REST HTTP requests and responses I used to build a proxy server and configure the proxy server on the client connection (my favorite is Mitm proxy) or you may use some sniffing tools like wireshark. Hope it might help, cheers.
Backend Developer at DigiSin
0

#3 User is offline   hehbhehb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 24-February 12

Posted 02 October 2017 - 08:23 PM

add content-type header?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users