4

I've created my custom controller and I want to return the result formated with JSON. I always get a JSON: []

Route:

 <route url="/V1/points/get" method="GET">
    <service class="module\Points\Controller\Points" method="get"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

Controller:

class Points extends Action implements \module\Points\Api\PointsInterface {
   protected $request;
   protected $resultJsonFactory;

protected $_pointFactory;

public function __construct(
    Context $context,
    \Magento\Framework\App\Request\Http $request,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
    \YellowOctopus\Points\Model\PointFactory $pointFactory
)
{
    parent::__construct($context);
    $this->request = $request;
    $this->_pointFactory = $pointFactory;
    $this->resultJsonFactory = $resultJsonFactory;
}

/**
 * @return \Magento\Framework\Controller\Result\JsonFactory
 */
public function get()
{
   try {
      (...)
//$result = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON);
        return $this->resultJsonFactory->create()
         ->setData([
            'act' => 'success',
            'totalResults' => 16,
            'results' => [],
        ]);
    } catch (\Exception $e) {
        $result = $this->resultJsonFactory->create();
        return $result->setData([
            'act' => 'error',
            'm' => $e->getMessage(),
        ]);
    }
}

/**
 * @return \Magento\Framework\Controller\Result\Json
 */
public function execute() {}

Request: GET: .../rest/V1/points/get?lat=50.164001&lng=-5.069998&distance=1500

Response (Postman): https://prntscr.com/hg738w.jpg

6
  • Where is your execute() function?
    – Nero Phung
    Commented Nov 28, 2017 at 7:02
  • execute() function is defined and empty Commented Nov 28, 2017 at 7:03
  • Was the get() function be called inside execute() function?
    – Nero Phung
    Commented Nov 28, 2017 at 7:10
  • what you , want to use magento rest api?
    – Amit Bera
    Commented Nov 28, 2017 at 7:10
  • The get() is called from routes Commented Nov 28, 2017 at 7:22

2 Answers 2

2

I added this code to function and now work fine

$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody(\Zend_Json::encode($response));
$this->getResponse()->sendResponse();
exit;
1
  • Where did you add those code? Commented Dec 27, 2017 at 13:47
0

Create an array like below and then return JSON response :

public function execute()
{
    $response = array();
    $response['act'] = 'success';
    $response['totalResults'] = '16';
    $response['results'] = [];

    $resultJson = $this->resultJsonFactory->create();
    return $resultJson->setData($response);
}
4
  • that not work for me. I don`t why. I added more details to my post Commented Nov 28, 2017 at 7:21
  • I added this code to get() and execute() Commented Nov 28, 2017 at 7:23
  • magento ver 1.9.2.3, I am work with REST API in POSTMAN, i have all oAuth 1.0 details like Consumer Key,Consumer Secret,Access Token,Token Secret. Now i moved to oAuth 2.0, how can i get my all oAuth 2.0 details? @Manashvi Birla
    – zus
    Commented Oct 30, 2018 at 6:27
  • How do i get totalResults value in js, imean in response
    – Jaisa
    Commented Dec 18, 2018 at 13:01

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.