2

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.

2 Answers 2

2

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

1
  • 1
    Perfect. I wasn't clear in my question, but other endpoints wore working correctly. It just wasn't clear to me how to use searchCriteria (I was missing the condition_type)
    – romaia
    Dec 10, 2017 at 13:06
1

try this buddy,

/V1/products/attribute-sets/groups/list?searchCriteria[filterGroups][0][filters][0][field]=attribute_set_id&searchCriteria[filterGroups][0][filters][0][value]={your attribute-set Id}

It'll work for sure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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