0

I am working on a custom API in which user will pass the request like below in JSON.

{ "cartItem": [ { "sku": "11223344", "qty": 1 }, { "sku": "55667788", "qty": 1 } ] }

I tried to get it done like this.

interface CartItemInterface extends CustomAttributesDataInterface
{
    const CART_ITEM = 'cartItem';

    /**
    * Return list of Cart Items
    * @return \Test\TestApi\Api\Data\CartItemListInterface[]
    */
    public function getCartItem();

    /**
    * @param array $cartItemList
    */
    public function setCartItem(array $cartItemList);


}

But it gives me this error every time, after so many changes in the logic.

"message": "Property "0" does not have accessor method "get0" in class Test\TestApi\Api\Data\CartItemInterface,

Please help me with code how to fix this problem and receive this request in which multiple array of key value pairs.

2 Answers 2

0

Your API interface should not extend the class CustomAttributesDataInterface, remove extend code and check if your issue resolve by that.

Sample API Interface Code:

file: app/code/Test/TestApi/Api/CartitemManagementInterface.php

<?php
declare(strict_types=1);

namespace Test\TestApi\Api;

interface CartitemManagementInterface
{

    const CART_ITEM = 'cartItem';

    /**
    * Return list of Cart Items
    * @return \Test\TestApi\Api\Data\CartItemListInterface[]
    */
    public function getCartItem();

    /**
    * @param array $cartItemList
    */
    public function setCartItem(array $cartItemList);
}
3
  • No, I removed the extend but still the same error. Can you please share how you have done it at your end. Thanks. Because i just need to receive array at my end when someone pass JSON array like this in request. "cartItem": [ { "sku": "11223344", "qty": 1 }, { "sku": "55667788", "qty": 1 } ] Commented Nov 23, 2020 at 11:23
  • Try this module generator to get API code mage2gen.com Commented Nov 23, 2020 at 12:14
  • Also, if you need json in request, then just use string, not array. from string, you can use json_decode to get the array Commented Nov 23, 2020 at 12:15
-1

For anyone else looking for answer, I have done similar stuff and here is my solution to receive 2D array in POST Request, you need Request Interface

namespace [Vendor]\[Namespace]\Api;
use [Vendor]\[Namespace]\Api\RequestInterface;

interface YourInterface
{
    /**
     * @param RequestInterface[] $cartItem
     * @return string
     */
 
    public function getPost(array $cartItem);
}

Create file [Vendor][Namespace]\Api\RequestInterface

namespace [Vendor]\[Namespace]\Api;
interface RequestInterface
{
    const QUANTITY = 'qty';
    const SKU = 'sku';

    /**
     * @return int
     */
    public function getQty(): int;

    /**
     * @param int $qty
     * @return void
     */
    public function setQty(int $qty): void;

    /**
     * @return string
     */
    public function getSku(): string;

    /**
     * @param string $sku
     * @return void
     */
    public function setSku(string $sku): void;
}

Create File [Vendor][Namespace]\Model\Api\Request

namespace [Vendor]\[Namespace]\Model\Api;

use [Vendor]\[Namespace]\Api\RequestInterface;
use Magento\Framework\DataObject;

class Request extends DataObject implements RequestInterface
{
    /**
     * @inheriDoc
     */
    public function getQty(): int
    {
        return (int)$this->getData(self::QUANTITY);
    }

    /**
     * @inheriDoc
     */
    public function setQty(int $qty): void
    {
        $this->setData(self::QUANTITY, $qty);
    }

    /**
     * @inheriDoc
     */
    public function getSku(): string
    {
        return $this->getData(self::SKU);
    }

    /**
     * @inheriDoc
     */
    public function setSku(string $sku): void
    {
        $this->setData(self::SKU, $sku);
    }

}

di.xml should be

<?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="[Vendor]\[Namespace]\Api\YourInterface" type="[Vendor]\[Namespace]\Model\Api\YourModel"/>
    <preference for="[Vendor]\[Namespace]\Api\RequestInterface" type="[Vendor]\[Namespace]\Model\Api\Request"/>
</config>

This will accept request as

{"cart_item":{"0":{"sku":"SKU1"},"1":{"sku":"SKU2"}}}

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.