1

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?

  • have you added this <preference for="Netzwelt\Booking\Api\BookingLocationListInterface" type="Netzwelt\Booking\Model\BookingLocationList" /> in di.xml under Netzwelt/Booking/etc/? – kunj Aug 8 '18 at 5:54
  • No, I didn't add this . Is it require ? – Atul Aug 8 '18 at 5:58
  • I added but it still givving the same issue..... – Atul Aug 8 '18 at 6:01
  • yes, required and after that you may need run php bin/magento setup:di:compile Please share that di.xml file code. – kunj Aug 8 '18 at 6:05
  • Yes did the same, I have added the di.xml file code in question . Please check – Atul Aug 8 '18 at 6:10
1

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;
    }
}
  • Yes did the same, But again its giving same issue... – Atul Aug 8 '18 at 6:12
  • One more thing, If I am running it via Curl its giving 404 in return – Atul Aug 8 '18 at 6:14
  • 1
    $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $baseUrl.'/V1/netzwelt/booking'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); – Atul Aug 8 '18 at 6:15
  • @Atul, I have updated the answer with core PHP code. – kunj Aug 8 '18 at 6:20
  • Okay let me try... – Atul Aug 8 '18 at 6:20

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.