Magento, Magento 2

Magento 2 Rest API Get All Post Params

Magento 2 Rest API Get All Post Params

In this tutorial, Today I will explain to how to get all post params of rest API in Magento 2. Rest API is useful to integrate and pull data from third party. Magento is by default provide many API like Product, Order, Customer etc. We can also create custom rest api to perform action in custom module also.

Now, If we want to get params or body content from Rest API then, what should we need to do for that? Let’s follow the steps of how to get all post params in Magento 2.

Step of Magento 2 Rest API Get All Post Params :

You may also like this :

You need to add this below code in your Model file to get all post params :

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Created By : Rohan Hapani
 */
namespace RH\CustomAPI\Model;

class GetProductCustomAPI
{
    /**
     * @var \Magento\Framework\Webapi\Rest\Request
     */
    protected $request;

    /**
     * @param \Magento\Framework\Webapi\Rest\Request $request
     */
    public function __construct(
        \Magento\Framework\Webapi\Rest\Request $request
    ) {
        $this->request = $request;
    }

    public function getBodyData()
    {
        $bodyParams = $this->request->getBodyParams(); // It will return all params which will pass from body of postman.
        return $bodyParams;
    }
}

It will return all params which you pass in body of postman for rest API. If you want to get single param value then, you can use this below line :

$yourParam = $this->request->getParam('your_param');

I hope this blog will helpful for easily understand about how to get all post params in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged , ,