Below are the custom api module with key pair value
To get JSON response, in client set Response headers to "Content-Type: application/json; charset=utf-8"
and if you need key pair value that is in response should have key and value as like /rest/V1/categories
we need to create data interface
To test in Chrome download plugin called rest client app call the url
below module url will be http://yourdomein.com/magento2/rest/V1/getinfo
app\code\Sugarcode\Customapi\registration.php:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Sugarcode_Customapi',
__DIR__
);
app\code\Sugarcode\Customapi\etc\module.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Sugarcode_Customapi" setup_version="2.0.0"/>
</config>
app\code\Sugarcode\Customapi\etc\di.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Sugarcode\Customapi\Api\TestInterface"
type="Sugarcode\Customapi\Model\Test" />
<preference for="Sugarcode\Customapi\Api\Data\TestdataInterface" type="Sugarcode\Customapi\Model\Testmodel" />
</config>
app\code\Sugarcode\Customapi\etc\webapi.xml:
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<!-- Example: curl http://127.0.0.1/index.php/rest/V1/calculator/add/1/2 -->
<route url="/V1/getinfo" method="GET">
<service class="Sugarcode\Customapi\Api\TestInterface" method="getinfo" />
<resources>
<resource ref="anonymous" />
</resources>
</route>
</routes>
app\code\Sugarcode\Customapi\Api\TestInterface.php:
<?php
namespace Sugarcode\Customapi\Api;
use Sugarcode\Customapi\Api\Data\TestdataInterface;
interface TestInterface
{
/**
* Retrieve list of info
*
* @throws \Magento\Framework\Exception\NoSuchEntityException If ID is not found
* @return \Sugarcode\Customapi\Api\Data\TestdataInterface containing Tree objects
*/
public function getinfo();
}
don't remove comments its most important
app\code\Sugarcode\Customapi\Api\Data\TestdataInterface.php (set and get data):
<?php
namespace Sugarcode\Customapi\Api\Data;
/**
* @api
*/
interface TestdataInterface
{
/**
* Get name
*
* @return string
*/
public function getName();
/**
* Set name
*
* @param string $name
* @return $this
*/
public function setName($id);
}
app\code\Sugarcode\Customapi\Model\Test.php:
<?php
namespace Sugarcode\Customapi\Model;
use Sugarcode\Customapi\Api\TestInterface;
/**
* Defines the implementaiton class of the calculator service contract.
*/
class Test implements TestInterface
{
/**
* Return the sum of the two numbers.
*
* @api
* @param int $num1 Left hand operand.
* @param int $num2 Right hand operand.
* @return int The sum of the two values.
*/
protected $dataFactory;
public function __construct(\Sugarcode\Customapi\Api\Data\TestdataInterfaceFactory $dataFactory)
{
$this->dataFactory = $dataFactory;
}
public function getinfo() {
$page_object = $this->dataFactory->create();
$page_object->setName('Hello');
return $page_object;
}
}
app\code\Sugarcode\Customapi\Model\Testmodel.php:
<?php
namespace Sugarcode\Customapi\Model;
class Testmodel extends \Magento\Framework\Model\AbstractModel implements
\Sugarcode\Customapi\Api\Data\TestdataInterface
{
const KEY_NAME = 'name';
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
public function getName()
{
return $this->_getData(self::KEY_NAME);
}
/**
* Set name
*
* @param string $name
* @return $this
*/
public function setName($name)
{
return $this->setData(self::KEY_NAME, $name);
}
}
Magento2 supports two formats of the messages for REST APIs: json and xml. Let's take json as an example: the api call for the service you created will looks like this:
curl -X POST "http://magento.url/rest/V1/vakri/update" -H "Content-Type: application/json" -d '{"xml": "<OrderEventNotification Id="E201602261456522075"> .. "}'
As you see here, you should have field, "xml", which corresponds to input parameter name of the method.
For XML it might be a bit more tricky, I would assume something like
curl -X POST "http://magento.url/rest/V1/vakri/update" -H "Content-Type: application/xml" -d '<xml>"<OrderEventNotification Id="E201602261456522075"> .. "</xml>'
In any case, there will be a problem if xml code contains characters which can break the request json/xml structure. One way to overcome that is to obfuscate xml string with some encoding, let's say base64, or escape all dangerous characters. In API service implementation you would need to do reverse operation.
Another, and much better way, is to actually create the Data Object, which represents the data from the XML file. Instead of sending raw XML string, you'll just use possibilities of Web API framework to serialize/deserialize data.
namespace Ezprints\Valkyrie\Model\Api\Data;
interface OrderEventNotificationInterface
{
/**
* @return string
*/
public function getId();
/**
* @return string
*/
public function getOrderId();
/**
* @return string
*/
public function getEZPReferenceNumber();
/**
* @return string
*/
public function getAcceptedDateTime();
}
namespace Ezprints\Valkyrie\Model\Api;
interface ValkyrieStatusUpdateInterface
{
/**
* Parse the notification XML and handle accordingly
*
* @api
* @param Data\OrderEventNotificationInterface $orderNotification
* @return Data\OrderEventNotificationResponseInterface
*/
public function processVaklyrieStatusUpdate(Data\OrderEventNotificationInterface $orderNotification);
}
API call will looks like:
curl -X POST "http://magento.url/rest/V1/vakri/update" -H "Content-Type: application/xml" -d '<id>E201602261456522075</id><order_id> 000000024</order_id><e_z_p_reference_number>0-01-7899-201602242228-0027-3808</e_z_p_reference_number><accepted_date_time>2016-02-26T21:27:55<accepted_date_time>'
Best Answer
I have been there so i used this approach and this works for me
try to create XML in variable like this
then final format of XML in another variable
then send to API like this
Hope this helps, upvote if it works for you