Error is "message": "Internal Error. Details are available in Magento log file. Report ID: webapi-5dee36e254394".
When you run http://localhost:8888/magento/rest/V1/position/1
Need to get result from "position" column.
Api is for retrieving a column field by given user input.
<?php
namespace SimpleMagento\Custom\Model;
use SimpleMagento\Custom\Api\InfoLinkInterface;
use SimpleMagento\Custom\Model\ResourceModel\Info\CollectionFactory; //Collection file
use SimpleMagento\Custom\Model\InfoFactory; //Implemented getter setter model
class InfoLink implements InfoLinkInterface
{
private $collection;
private $infoFactory;
public function __construct(CollectionFactory $collection,InfoFactory $infoFactory)
{
$this->collection=$collection;
$this->infoFactory=$infoFactory;
}
/**
* @param int $id
* @return \SimpleMagento\Custom\Api\Data\InfoInterface[]
*/
public function getInfo($id)
{
return $this->infoFactory->create()->getCollection()->addFieldToFilter('entity_id',$id)->getData();
//return $this->infoFactory->create()->getData('position')->addFieldToFilter('entity_id',1);
}
}
?>
webapi.xml is
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/position/:id" method="GET">
<service class="SimpleMagento\Custom\Api\InfoLinkInterface" method="getInfo"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>
2 Answers 2
It seems like you added wrong model file class name in your di.xml file :
Change from :
SimpleMganeto\Custom\Model\InfoLink
To :
SimpleMagento\Custom\Model\InfoLink
-
Thanks a lot for correction but not able to get, even after that. Code flow of developing API is corect, right? . To make it more simple I ll write SQL query , from which is I want to get result. **SELECT POISITION FROM NEW INFO WHERE ENTITY_ID=$ID **Akash Killedar– Akash Killedar2019年12月10日 04:10:05 +00:00Commented Dec 10, 2019 at 4:10
-
You added addFieldToFilter with entity_id. I think it should be position.Rohan Hapani– Rohan Hapani2019年12月10日 04:23:34 +00:00Commented Dec 10, 2019 at 4:23
-
what you get data in getInfo function?Rohan Hapani– Rohan Hapani2019年12月10日 04:24:34 +00:00Commented Dec 10, 2019 at 4:24
-
just a string from position columnAkash Killedar– Akash Killedar2019年12月10日 04:27:40 +00:00Commented Dec 10, 2019 at 4:27
-
If there will be unique record in your table. Then, you can use $record = $this->infoFactory->create()->getCollection()->addFieldToFilter('entity_id',$id)->getLastItem(); ................ and then you can return, $record->getPosition(); or $record['position'];Rohan Hapani– Rohan Hapani2019年12月10日 04:30:46 +00:00Commented Dec 10, 2019 at 4:30
Try to replace this line
return $this->infoFactory->create()->getCollection()->addFieldToFilter('entity_id',$id)->getData();
With this one
return $this->infoFactory->create()->getCollection()->addFieldToFilter('entity_id',array('eq', $id))->getFirstItem()->getPosition();
Follow this link for reference.
Hope this will help you!
-
Let me know if you need any help then. ;)Kishan Savaliya– Kishan Savaliya2019年12月10日 07:30:54 +00:00Commented Dec 10, 2019 at 7:30
-
we just need to add ** localhost:8888/magento/rest/V1/position/1 ** This in postman right? So that :id=1Akash Killedar– Akash Killedar2019年12月10日 08:03:19 +00:00Commented Dec 10, 2019 at 8:03
-
Yes, @AkashKilledar. RightKishan Savaliya– Kishan Savaliya2019年12月10日 08:38:18 +00:00Commented Dec 10, 2019 at 8:38
-
but error in postman is "message": "Server internal error. See details in report api/617778416808" :- 'Uncaught Error: Cannot instantiate interface.Akash Killedar– Akash Killedar2019年12月10日 09:08:54 +00:00Commented Dec 10, 2019 at 9:08
-
Can you please send me full error ?Kishan Savaliya– Kishan Savaliya2019年12月10日 09:13:46 +00:00Commented Dec 10, 2019 at 9:13
2019年12月09日 12:57:41] main.CRITICAL: Report ID: webapi-5dee44c52bdb3; Message: Class SimpleMganeto\Custom\Model\InfoLink does not exist {"exception":"[object] (Exception(code: -1): Report ID: webapi-5dee44c52bdb3; Message: Class SimpleMganeto\\Custom\\Model\\InfoLink does not exist at /Applications/MAMP/htdocs/magento/vendor/magento/framework/Webapi/ErrorProcessor.php:205, ReflectionException(code: -1): Class SimpleMganeto\\Custom\\Model\\InfoLink does not exist at /Applications/MAMP/htdocs/magento/vendor/magento/framework/Code/Reader/ClassReader.php:19)"} []