How to get default JEST REST API response in Magento

In magento, since we use a REST URL to access data, like http://localhost/magemto/api/rest/products it is returned in XML format.

But as a requirement of my team, I have to send data in JSON format for quick access to AJAX calls. I used the REST client to include the header as "Content-Type: appilcation / json". Then it returns in JSON format .. But I want it to be the default magento API.

+4
source share
1 answer

Hey, I have a solution for this, I would like to share with you.

Go to the root folder of magento first, then go to the next path

\app\code\core\Mage\Api2\Model\Request.php

Go to the getAccepTypes () method and change it using this code below, it will fulfill your requirements.

 public function getAcceptTypes() { $qualityToTypes = array(); $orderedTypes = array(); foreach (preg_split('/,\s*/', $this->getHeader('Accept')) as $definition) { $typeWithQ = explode(';', $definition); $mimeType = trim(array_shift($typeWithQ)); // check MIME type validity if (!preg_match('~^([0-9a-z*+\-]+)(?:/([0-9a-z*+\-\.]+))?$~i', $mimeType)) { continue; } $quality = '1.0'; // default value for quality if ($typeWithQ) { $qAndValue = explode('=', $typeWithQ[0]); if (2 == count($qAndValue)) { $quality = $qAndValue[1]; } } $qualityToTypes[$quality][$mimeType] = true; } krsort($qualityToTypes); foreach ($qualityToTypes as $typeList) { $orderedTypes += $typeList; } unset($orderedTypes); $orderedTypes=Array ("application/json" => 1); return array_keys($orderedTypes); } 

Hope this helps you.

+12
source

All Articles