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

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

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;
  • Where did you add those code? – Hiranya Sarma Dec 27 '17 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);
}
  • that not work for me. I don`t why. I added more details to my post – Krzysztof Gwiazda Nov 28 '17 at 7:21
  • I added this code to get() and execute() – Krzysztof Gwiazda Nov 28 '17 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 Oct 30 '18 at 6:27
  • How do i get totalResults value in js, imean in response – Jaisa Dec 18 '18 at 13:01

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.