0

Magento 2 Core API to get products in GET method but want to make it POST method .

How to do it and also open API ?

1 Answer 1

1

in Your custom module create webapi.xml in etc folder and paste below code

<route url="/V1/products/lists" method="POST">
    <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="getList"/>
    <resources>
        <resource ref="anonymous" />
    </resources>
</route>

and below is the code to call API in curl

<?php

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

//Authentication rest API magento2.Please change url accordingly your url
//$url = 'http://YOURDOMAIN.COM/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=sku&searchCriteria[filter_groups][0][filters][0][value]=%simple%&searchCriteria[filter_groups][0][filters][0][condition_type]=like';


// POST Method 
$url = 'http://YOURDOMAIN.COM/rest/V1/products/lists';
$searchCriteria['searchCriteria']['filter_groups'][0]['filters'][0]['field']='sku';
$searchCriteria['searchCriteria']['filter_groups'][0]['filters'][0]['value']='%simple%';
$searchCriteria['searchCriteria']['filter_groups'][0]['filters'][0]['condition_type']='like';

$data_string = json_encode($searchCriteria);
echo '<pre>'; echo $data_string;echo '------<br>';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
);

$datas = curl_exec($ch);
echo $datas; 
$datas = json_decode($datas);
print_r($datas);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.