I am using rest services in magento 2. Using rest services i want to save data in my custom table. To get the post data i am writing following code but it's not getting the posted date in API. It givens passed array instead of string. How can i post JSON array in REST api and save get that posted data in model method?
$this->request->getPost() or $this->request->getPostValues(); not working and throwing error.
EnquiryInterface.php file code:
namespace Comapny\Modulename\Api;
interface EnquiryInterface
{
public function getenquirydata($paramaters);
}
webapi.xml
<route url="/V1/company/enquiry" method="POST">
<service class="Comapny\Modulename\Api\EnquiryInterface" method="getenquirydata"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
di.xml
<preference for="Comapny\Modulename\Api\EnquiryInterface" type="Comapny\Modulename\Api\Enquiry" />
Model/Enquiry.php
namespace Comapny\Modulename\Model;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\App\RequestInterface;
class Enquiry extends \Magento\Framework\Model\AbstractModel
{
protected $request;
public function __construct(
RestRequest $request,
){
$this->request = $request;
}
public function getenquirydata($paramaters){
$post= $this->request->getPost();
return $paramaters;
}
}
Hi @jacktorris
okay i have seen the code you posted and understand the actual issue !
From your Model when you are returning parameters at that time you required to send data in json_encode format.
Below is the full modified code for your model file :
Model/Enquiry.php
namespace Comapny\Modulename\Model; use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\App\RequestInterface; class Enquiry extends \Magento\Framework\Model\AbstractModel { protected $request; public function __construct( RestRequest $request, ){ $this->request = $request; } public function getenquirydata($paramaters){ $post= $this->request->getPost(); // if you are trying to return $post over here then you need to encode post data into json like below : $post = json_encode($this->request->getPost()); // if you are trying to return $paramaters; over here then you need to encode post data into json like below : $paramaters; = json_encode($this->request->getPost()); return $paramaters; } }
Then check it will works and returns data in string format !
Hope it helps
I am already posting the data in json format through postman in following format:
{
paramters:{
"name":"Test",
"Phone":"25423423423",
"email" : "test@test.com"
}
}
Posting data in above format despite when its not showing data in model.
In mode if i try $this->request->getParam() or $this->request->getPost() it doesn't show the paramaters posted through postman body,.
use below code will give you body parameters which we have pass as a post data
/** * @var \Magento\Framework\Webapi\Rest\Request */ protected $request; /** * constructor * * @param \Magento\Framework\Webapi\Rest\Request $request */ public function __construct( \Magento\Framework\Webapi\Rest\Request $request ) { $this->request = $request; } /** * {@inheritdoc} */ public function getPostParams() { return $this->request->getBodyParams(); }
This works for REST for SOAP
$this->request->getContent()
Worked well!