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.

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 } ] – Shahzad H. Nov 23 '20 at 11:23
  • Try this module generator to get API code mage2gen.com – Shoaib Munir Nov 23 '20 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 – Shoaib Munir Nov 23 '20 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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