1

I am trying to create a simple module to return JSON data, if i returned string or array it will return the targeted data, but i can't return JSON object with key value

when i return the interface it will return error 500, the second time i refresh it will keep loading forever. so i have to restart apache to work.

so my code, first the product interface:

<?php
namespace MzTest\ApiTool\Api\Data;

interface ProductInterface
{
    /**
     * @return int
     */
    public function getId();
    /**
     * @param int $id
     * @return $this
     */
    public function setId($id);

    /**
     * @return string
     */
    public function getName();
    /**
     * @param string $name
     * @return $this
     */
    public function setName($name);
}

the used model:

<?php

namespace MzTest\ApiTool\Model\Data;

use Magento\Framework\DataObject;
use MzTest\ApiTool\Api\Data\ProductInterface;

class Product extends DataObject implements ProductInterface
{
    /**
     * @inheritdoc
     */
    public function getId()
    {
        $this->getId();
        return $this->getData('id');
    }
    /**
     * @inheritdoc
     */
    public function setId($id)
    {
        return $this->setData('id', $id);
    }

    /**
     * @inheritdoc
     */
    public function getName()
    {
        return $this->getData('name');
    }
    /**
     * @inheritdoc
     */
    public function setName($name)
    {
        return $this->setData('name', $name);
    }
}

and in the method:

/**
 * @api
 * @param int $id
 * @return \MzTest\ApiTool\Api\Data\ProductInterface
 * @throws NoSuchEntityException
 */
public function show($id)
{
    /** @var \MzTest\ApiTool\Api\Data\ProductInterface $productInterface */
    $productInterface = $this->productInterfaceFactory->create();

    try {
        /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
        $product = $this->productRepository->getById($id);

        $productInterface->setId($product->getId());
        $productInterface->setSku($product->getSku());
        $productInterface->setName($product->getName());

        return $productInterface;
       
    } catch (NoSuchEntityException $th) {
        throw NoSuchEntityException::singleField('id', $id);
    }
}

Update:

ProductRepositoryInterface:

<?php
namespace MzTest\ApiTool\Api;

use Magento\Framework\Exception\NoSuchEntityException;

interface ProductRepositoryInterface
{
    /**
     * Undocumented function
     *
     * @api
     * @param int $id
     * @return \MzTest\ApiTool\Api\Data\ProductInterface
     * @throws NoSuchEntityException
     */
    public function show($id);

}

2 Answers 2

1

After Searching I found the issue was from serializing the object, I was extening DataObject in class Product. and it doesn't have __toArray method.

the solution was to extend Magento\Framework\Api\AbstractSimpleObject

0

Update:

ProductRepositoryInterface:

<?php
namespace MzTest\ApiTool\Api;

use Magento\Framework\Exception\NoSuchEntityException;

interface ProductRepositoryInterface
{
    /**
     * Undocumented function
     *
     * @api
     * @param int $id
     * @return string
     * @throws NoSuchEntityException
     */
    public function show($id);

}

You need to encode the API response and then return like below.

    /**
     * @api
     * @param int $id
     * @return \MzTest\ApiTool\Api\Data\ProductInterface
     * @throws NoSuchEntityException
     */
    public function show($id)
    {
        /** @var \MzTest\ApiTool\Api\Data\ProductInterface $productInterface */
        $productInterface = $this->productInterfaceFactory->create();
    
        try {
            /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
            $product = $this->productRepository->getById($id);
    
            $productInterface->setId($product->getId());
            $productInterface->setSku($product->getSku());
            $productInterface->setName($product->getName());
            
            //return json_encode($productInterface); /*Modified this line in your code.*/

echo json_encode($productInterface); exit; /*Modified this line in your code.*/

           
        } catch (NoSuchEntityException $th) {
            throw NoSuchEntityException::singleField('id', $id);
        }

}
6
  • it does return "{}"with quotation Jan 20, 2023 at 15:16
  • can you share your interface file in the question or refer to this return string line? please check this screenshot. tinyurl.com/2eukhlks Jan 20, 2023 at 15:43
  • I have again updated the return line in my answer. can you try that? Jan 20, 2023 at 15:49
  • I have updated my code, and have tested echo and exit, but i can't set the headers to to json Jan 20, 2023 at 16:01
  • You can set the JSON in headers using the postman tool like this screenshot. tinyurl.com/2ls5mcka Also I have made a change in the interface class for return line, please check my updated answer. Jan 20, 2023 at 16:20

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.