Magento – Magento 2 : REST API response

magento2rest api

I created a custom module to manage new entity. I developed for this module a REST API endpoint to get this entity by ID. My issue is when I'm sending the data I'm using json_encode but it does not send the data as the other default endpoints, how should I send back the data ?

    $attachmentData = [];
    if($attachment->getData()) {
        $attachmentData = [
            'id' => $attachment->getId(),
            'name' => $attachment->getName(),
            'description' => $attachment->getDescription()
        ];
    }

    return json_encode($attachmentData);

Thanks !

Best Answer

Here is an example that may help you. In this example package name is Henote and module name is Store:

step1 : 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="GET" url="/V1/store/storeDetails">
            <service class="Henote\Store\Api\StoreManagementInterface" method="getStoreDetails"/>
            <resources>
                <resource ref="anonymous"/>
            </resources>
        </route>    
    </routes>

step2: 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="Henote\Store\Api\StoreManagementInterface" type="Henote\Store\Model\StoreManagement"/>
        <preference for="Henote\Store\Api\Data\StoreDetailsInterface" type="Henote\Store\Model\StoreDetails"/>
    </config>

step3:Henote\Store\Api\StoreManagementInterface.php

    <?php
    namespace Henote\Store\Api;

    interface StoreManagementInterface
    {
        /**
         * GET for getStoreDetails api
         * @param int $id
         * @return \Henote\Store\Api\Data\StoreDetailsInterface
         */
        public function getStoreDetails($id);

    }

step4: Henote\Store\Model\StoreManagement.php

    <?php
    namespace Henote\Store\Model;

    use Henote\Store\Api\StoreManagementInterface;
    use Magento\Framework\ObjectManagerInterface;
    use Henote\Store\Api\Data\StoreDetailsInterfaceFactory;

    class StoreManagement implements StoreManagementInterface
    {

        protected $objectManager;

        protected $storedetailsFactory;


        /**
         * @param ObjectManagerInterface $objectManager     
         * @param StoreDetailsInterfaceFactory $storedetailsFactory
         */
        public function __construct(
            ObjectManagerInterface $objectManager,
            StoreDetailsInterfaceFactory $storedetailsFactory
        ) {
            $this->objectManager = $objectManager;
            $this->storedetailsFactory = $storedetailsFactory;
        }



        /**
         * GET for getStoreDetails api
         * @param int $id
         * @return \Henote\Store\Api\Data\StoreDetailsInterface
         */
        public function getStoreDetails($id){
            $model = $this->objectManager->create('Henote\Store\Model\Store')->load($id);
            $details = $this->storedetailsFactory->create();
            if($model->getId()){
                $details->setId($model->getId());
                $details->setName($model->getName());
                $details->setDescription($model->getDescription());
                return $details;
            }
            else{
                return false;
            }

        }       
    }

step5:Henote\Store\Api\Data\StoreDetailsInterface.php

    <?php
    namespace Henote\Store\Api\Data;

    interface StoreDetailsInterface
    {
        /**
         * Get store id
         * @return int
         */
        public function getId();

        /**
         * Set store id
         * @param int $id
         * @return $this
         */
        public function setId($id);

        /**
         * Get store name
         * @return string|null
         */
        public function getName();

        /**
         * Set store name
         * @param string $name
         * @return $this
         */
        public function setName($name);

        /**
         * Get Description
         * @return string|null
         */
        public function getDescription();

        /**
         * Set Description
         * @param string $description
         * @return $this
         */
        public function setDescription($description);

    }

step6: Henote\Store\Model\StoreDetails.php

    <?php
    namespace Henote\Store\Model;

    use Henote\Store\Api\Data\StoreDetailsInterface;

    class StoreDetails extends \Magento\Framework\Api\AbstractSimpleObject implements StoreDetailsInterface
    {
        const KEY_ID = 'id';
        const KEY_NAME = 'name';
        const KEY_DESCRIPTION = 'description';


        /**
         * Get store id
         * @return int
         */
        public function getId()
        {
            return $this->_get(self::KEY_ID);
        }    

        /**
         * Set store id
         * @param int $id
         * @return $this
         */
        public function setId($id)
        {
            return $this->setData(self::KEY_ID, $id);
        }

        /**
         * Get store name
         * @return string|null
         */
        public function getName()
        {
            return $this->_get(self::KEY_NAME);
        } 

        /**
         * Set store name
         * @param string $name
         * @return $this
         */
        public function setName($name)
        {
            return $this->setData(self::KEY_NAME, $name);
        }

        /**
         * Get Description
         * @return string|null
         */
        public function getDescription()
        {
            return $this->_get(self::KEY_DESCRIPTION);
        } 


        /**
         * Set Description
         * @param string $description
         * @return $this
         */
        public function setDescription($description)
        {
            return $this->setData(self::KEY_DESCRIPTION, $description);
        }
    }
Related Topic