My webapi.xml is below.
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/garage/insert/" method="POST">
<service class="Garage\Motogaadi\Api\GarageInsert" method="insert"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
Also my di.xml is below
<?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="Garage\Motogaadi\Api\GarageInsert" type="Garage\Motogaadi\Model\GarageAdd" />
</config>
My interface is below.
<?php
namespace Garage\Motogaadi\Api;
interface GarageInsert
{
/**
* Returns greeting message to user
*
* @api
* @param string $data Users name.
* @return string Greeting message with users name.
*/
public function insert($data);
}
My model is below
<?php
namespace Garage\Motogaadi\Model;
use Garage\Motogaadi\Api\GarageInsert;
class GarageAdd implements GarageInsert
{
/**
* Returns greeting message to user
*
* @api
* @param string $data Users name.
* @return string Greeting message with users name.
*/
public function insert($data)
{
//Customize the code as per your requirement.
$cid = $data['id'];
$gid = $data['gid'];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('garage_customer_ids');
$sql = "Insert Into " . $tableName . " ( customer_id, garage_data_id) Values ('$cid','$gid')";
$connection->query($sql);
return 'successfully saved';
}
}
I get the below error when I post the data.
"message": "The \"array\" value's type is invalid. The \"string\" type was expected. Verify and try again.",