Magento 2 – How to Fix REST API Error 500: Invalid Parameter Type

apimagento2

I am trying to create an API in Magento2 mine custom Module. But its giving 500 : {"message":"Invalid parameter type \"\\Netzwelt\\Booking\\Model\\BookingLocationList\" error, its coming when as I m opening swagger.

Here is mine code –

app\code\Netzwelt\Booking\Api\BookingLocationListInterface.php

namespace Netzwelt\Booking\Api;

interface BookingLocationListInterface
{
    /**
     * @return \Netzwelt\Booking\Model\BookingLocationList
     */
     public function getBookingLocationList();
}

app\code\Netzwelt\Booking\Model\BookingLocationList.php

namespace Netzwelt\Booking\Model;

use Magento\Framework\ObjectManagerInterface;

class BookingLocationList extends \Magento\Framework\Model\AbstractModel
{
    protected $objectmanager;

    public function __construct(
        ObjectManagerInterface $objectmanager        
    ) {
        $this->_objectManager = $objectmanager;
    }

    public function getBookingLocationList(){       
        $data[] = 1;
        $data[] = 2;
        $data[] = 3;
        $data[] = 4;
        return $data;
    }
}

app\code\Netzwelt\Booking\etc\webapi.xml

<?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 method="GET" url="/V1/netzwelt_booking">
        <service class="Netzwelt\Booking\Api\BookingLocationListInterface" method="getBookingLocationList"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

app\code\Netzwelt\Booking\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="Netzwelt\Booking\Api\BookingLocationListInterface" type="Netzwelt\Booking\Model\BookingLocationList" />
</config>

enter image description here

Please check and let me know what I am doing wrong?

Best Answer

di.xml file which will be at Netzwelt/Booking/etc/ and code should be like this.

<?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="Netzwelt\Booking\Api\BookingLocationListInterface" type="Netzwelt\Booking\Model\BookingLocationList" />
</config>

you may need run this command php bin/magento setup:di:compile and php bin/magento cache:flush as we worked with XML.

In PHP try this code for calling.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$userData = array("username" => "{{admin user name}}", "password" => "{{admin user password}}");
$url = $baseUrl."/rest/";
$ch = curl_init($url."V1/integration/admin/token");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);
/*---------------------------------------------- netzwelt booking ---------------------------------------------- */

$get_params = "";
$ch = curl_init($url."V1/netzwelt_booking".$get_params);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
print_r(curl_error($ch));
$result = json_decode($result, 1);
echo '<pre>';print_r($result);
exit;

Please correct doc like this.

use Magento\Framework\ObjectManagerInterface;

class BookingLocationList extends \Magento\Framework\Model\AbstractModel
{
    protected $objectmanager;

    public function __construct(
        ObjectManagerInterface $objectmanager
    ) {
        $this->_objectManager = $objectmanager;
    }
    /**
     * @return \MagentoCoders\Api\Model\BookingLocationList
     */
    public function getBookingLocationList(){
        $data[] = 1;
        $data[] = 2;
        $data[] = 3;
        $data[] = 4;
        return $data;
    }
}