How to insert data with JSON ?

239 views
Skip to first unread message

Pascal JEGO

unread,
May 25, 2017, 10:19:21 AM5/25/17
to Fat-Free Framework
Hello, I'm new here and with fat free,
I have a question


How to insert a data with a input json ?

I use a plugin RESTClient in Firefox forn my tests. the error is 500 Internal Server Error
the data json RESTClient is

{
   
"title": "Another article",
   
"content": "This article was created using the JSON API. How cool is that?"
}


I use the post method. the header is Content-Type: application/json

i have defined in route.ini 

POST /api/article=ArticleController->apiCreateArticle



My function is

 
   public function apiCreateArticle($f3) {
     
        $filters
= array(
         
'art_title' => FILTER_SANITIZE_STRING,
         
'art_content' => FILTER_SANITIZE_STRING
       
);
       
if ($_REQUEST) {
           
// By default, use standard HTTP POST fields
            $post
= $_REQUEST;
       
} else {
           
return;
       
}
       
// Ensure a status ID is added
       
if (!empty($post["title"])) {
           
return;
       
}
       
if (!empty($post["content"])) {
           
return;
       
}

        $article
= new Article($this->db);
        $article
->set('art_title', $post["title"]);
        $article
->set('art_content', $post["content"]);
        $article
->save();
   
}


When i test with RESTClient my app can't even find the route 


thanks

ved

unread,
May 25, 2017, 4:06:52 PM5/25/17
to Fat-Free Framework
Try using:

$post = json_decode(file_get_contents('php://input'), true);


ikkez

unread,
May 25, 2017, 4:40:55 PM5/25/17
to Fat-Free Framework
I had that same issue yesterday.

make sure that your php.ini contains

enable_post_data_reading = On

and then use ved's method to read and get the POST data, because php doesn't recognize json-post, as it's used to receive a "application/x-www-form-urlencoded" Content-Type.
if you haven't enabled that php.ini value, then all the POST routes comes in as GET and you're not able to fetch its payload.

I basically used this snippet for automatically populating the POST array:



require
('vendor/autoload.php');
/** @var \Base $f3 */
$f3 = \Base::instance();

// transform json POST body data
if ($f3->VERB == 'POST'
&& preg_match('/json/',$f3->get('HEADERS[Content-Type]')))
{
$f3->set('BODY', file_get_contents('php://input'));
if (strlen($f3->get('BODY'))) {
$data = json_decode($f3->get('BODY'),true);
if (json_last_error() == JSON_ERROR_NONE)
$f3->set('POST',$data);
}
}

$f3->run();

Pascal JEGO

unread,
May 26, 2017, 5:38:47 AM5/26/17
to f3-fra...@googlegroups.com
Thank you for your answers

To make tests, i created a new application with the suplied code of Ikkez. 
I have no more errors message with RESTCient. 

But, i have a new question.   
How to show $data ?
With a var_dump ?
But or ?
Have to I create a new route ?

Thanks

Anatol

unread,
May 26, 2017, 5:45:08 PM5/26/17
to f3-fra...@googlegroups.com
well, POST should now hold your data means anywhere in your code
you can access it f.e.:

print_r($f3->get('POST'));

Reply all
Reply to author
Forward
0 new messages