1

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.",

  • Check my updated answer. This both option working for array. – Rohan Hapani Dec 10 '19 at 11:30
2

You just need to change parameter type in your interface. Set $data type "array" from "string" :

<?php
namespace Garage\Motogaadi\Api;

interface GarageInsert
{

    /**
     * Returns greeting message to user
     *
     * @api
     * @param array $data
     * @return string Greeting message with users name.
     */

    public function  insert($data);
}

You can add this one also : * @param string[] $data

  • Thanks. But I get this "message": "Class array does not exist", error. I tried both string[] and array – Ramesh KR Dec 10 '19 at 11:34
  • You need to change in model file also. Happy coding !! :) – Rohan Hapani Dec 10 '19 at 11:37
  • 1
    Annotation is the definition of calling function. so you must give the related type parameter. you are absolutely right @RohanHapani – Bharat Desai Dec 10 '19 at 12:28
2

Please remove generated directory and run below command once to fix type error occurred when creating object error

php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush

Hope this will help you!

  • thansk for your comment. I have edited my issue in this question would you pls check – Ramesh KR Dec 10 '19 at 11:01
  • Okay let me check – Kishan Savaliya Dec 10 '19 at 11:01
  • Can you please check what value you're getting in this variables ? $cid = $data['id']; $gid = $data['gid']; – Kishan Savaliya Dec 10 '19 at 11:02
  • Otherwise please set $cid =1;$gid =2; and check it will work – Kishan Savaliya Dec 10 '19 at 11:04
  • I have tried both but still I receive same error – Ramesh KR Dec 10 '19 at 11:09

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.