Magento 2 REST API – How to List Groups of an Attribute Set

apimagento2product-attributerest

I am trying to use the Magento 2 rest API to list the groups of an attribute set:

GET /V1/products/attribute-sets/groups/list

But the request fails with the following error:

{
  "message": "%fieldName is a required field.",
  "parameters": {
    "fieldName": "attribute_set_id"
  }
}

I can't seem to find how to pass the required field and there is no documentation on how that should be passed.

Best Answer

I'm not sure your URL request. But, if you want to call GET /V1/products/attribute-sets/groups/list, need an Admin token and add searchCriteria. For example:

<?php

$userData = ["username" => "admin", "password" => "admin123"];
$ch = curl_init("http://mage220.loc/rest/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);

$searchCondition = "searchCriteria[filter_groups][0][filters][0][field]=attribute_set_id&" ;
$searchCondition = $searchCondition .  "searchCriteria[filter_groups][0][filters][0][value]=1&" ;
$searchCondition = $searchCondition ."searchCriteria[filter_groups][0][filters][0][condition_type]=eq";

$ch = curl_init("http://mage220.loc/rest/V1/products/attribute-sets/groups/list/?" . $searchCondition);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // Request method
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);

$result = json_decode($result, 1);
echo '<pre>';print_r($result);

Result:

Array
(
    [items] => Array
        (
            [0] => Array
                (
                    [attribute_group_id] => 1
                    [attribute_group_name] => General
                    [attribute_set_id] => 1
                )

        )

    [search_criteria] => Array
        (
            [filter_groups] => Array
                (
                    [0] => Array
                        (
                            [filters] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => attribute_set_id
                                            [value] => 1
                                            [condition_type] => eq
                                        )

                                )

                        )

                )

        )

    [total_count] => 1
)

http://devdocs.magento.com/guides/v2.2/rest/performing-searches.html

Related Topic