CodeIgniter Forums
How can I send JSON When using FeatureTestCase? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How can I send JSON When using FeatureTestCase? (/showthread.php?tid=77210)



How can I send JSON When using FeatureTestCase? - cbw0916 - 08-02-2020

API Document example

Create member

Code:
POST /members HTTP/1.1
Host: localhost
Content-Type: application/json

{
    "name" : "mike",
    "age" : 30
}

MemberController.php
PHP Code:
<?php
class MemberController extends Controller
{
    function create()
    {
        $data $this->reqest->getJSON();  // <-- parse JSON
        // codes..
    }




In this case, I want to make test function like this
PHP Code:
<?php
class MemberTest extends FeatureTestCase
{
    public function testCreateMember() {
        $body = [
            'name' => 'mike',
            'age' => 30,
        ];
        $result $this->post('member/v2/organs'$body);
        // codes..
    }


It doesn't work.
How can I send JSON when FeatureTestCase ?


RE: How can I send JSON When using FeatureTestCase? - InsiteFX - 08-03-2020

Your MemberController is not extending the Controller class.


RE: How can I send JSON When using FeatureTestCase? - cbw0916 - 08-03-2020

(08-03-2020, 09:28 AM)InsiteFX Wrote: Your MemberController is not extending the Controller class.
I just didn't write this. Actually extending Controller class.
So, It doesn't matter.


RE: How can I send JSON When using FeatureTestCase? - kenjis - 02-27-2021

Use `withBodyFormat()`.

See https://codeigniter4.github.io/CodeIgniter4/testing/feature.html#formatting-the-request