Magento 2.3 – How to Pass JSON Encoded Array in Custom API

apicustommagento2magento2.3

I have created custom API that excepting 'mixed' type parameter and returning the 'string' as I defined. In interface I define parameter type 'mixed'. As I am trying to pass array as json_encode, it giving me Warning: json_decode() expects parameter 1 to be string, array given in error.` Please let me know how can I pass json array as parameter and return the same.

Here is my code –
etc/webapi.xml

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route method="POST" url="/V1/managecustomers">
        <service class="Namespace\Modulename\Api\ManageCustomerInterface" method="createUpdate"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

etc/di.xml –

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Namespace\Modulename\Api\ManageCustomerInterface" type="Namespace\Modulename\Model\Managecustomer"/>
</config>

Namespace\Modulename\Api\ManageCustomerInterface.php :-

namespace Namespace\Modulename\Api;

interface ManageCustomerInterface
{
    /**
     * 
     * @param mixed $data
     * @return string
     */
    public function createUpdate($data);
}

Namespace\Modulename\Model\Managecustomer.php – :

namespace Namespace\Modulename\Model;

use Namespace\Modulename\Api\ManageCustomerInterface;

class Managecustomer implements ManageCustomerInterface
{
    /**
     * 
     * @param mixed $data
     * @return string
     */
    public function createUpdate($data) {
        $data = json_decode($data);
        //echo "<pre>"; print_r($data); die;
        return "Hello";

    }
}

If i am passing 'data' parameter as string like –
$customerData = [
'data' => "Abcd"
];

Then its running fine.
But If i am trying to pass 'data' as array like-

$customerData = [
    'data' => [
        "email" => "user@example.com",
        "firstname" => "John",
        "lastname" => "Doe",
        "storeId" => 1,
        "websiteId" => 1,
        "address"=>[
            "street" => "phase-1",
            "phone_no"=> "321654213"
        ]
    ]
];

Then its giving error – Warning: json_decode() expects parameter 1 to be string, array given in error.`

Here is my testapi.php file code –

$ch = curl_init("http://localhost/mage23/index.php/rest/all/V1/managecustomers");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

$result = curl_exec($ch);

echo '<pre>';print_r($result);

Please let me know how can I pass array as parameter in API.

Best Answer

If you've added proper docblock and still getting this error, replace @param mixed to @param string[] or @param mixed[] as according to magento devdocs.

Valid scalar types include: mixed (or anyType), bool (or boolean), str (or string), integer (or int), float, and double.

Also note that you must always use fully qualified class name or a fully qualified interface name in docblock.

Reference :

https://devdocs.magento.com/guides/v2.2/extension-dev-guide/service-contracts/service-to-web-service.html

Related Topic