I need to create a product using a custom Rest API. (this is my first time working on API). So to check API whether it’s working or not I wrote the code below, API is working, now I need to give different parameters to create a simple product but I am not able to figure out how to do that. If you know how to do that please help me out.
app/code/Webkul/Marketplace/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">
<route method="POST" url="/V1/custom/custom-api/">
<service class="WebkulMarketplaceApiCustomInterface" method="getPost"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
app/code/Webkul/Marketplace/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="WebkulMarketplaceApiCustomInterface" type="WebkulMarketplaceModelApiCustom"/>
</config>
app/code/Webkul/Marketplace/Api/CustomInterface.php
<?php
namespace WebkulMarketplaceApi;
interface CustomInterface
{
/**
* GET for Post api
* @param string $value
* @return string
*/
public function getPost($value);
}
app/code/Webkul/Marketplace/Model/Api/Custom.php
<?php
namespace WebkulMarketplaceModelApi;
use PsrLogLoggerInterface;
class Custom
{
protected $logger;
public function __construct(
LoggerInterface $logger
)
{
$this->logger = $logger;
}
/**
* @inheritdoc
*/
public function getPost($value)
{
$response = ['success' => false];
try {
$response = ['success' => true, 'data' => $value];
} catch (Exception $e) {
$response = ['success' => false, 'message' => $e->getMessage()];
$this->logger->info($e->getMessage());
}
$returnArray = json_encode($response);
return $returnArray;
}
}