There has been similar issues posted without an answer unfortunately but I am posting two ways in the API to set a category on a product both of which give different errors.

The first is using the categories repository (catalogCategoryLinkRepositoryV1)

example In body POST

    "productLink" :
        "sku" : "TEST_SKU5",  
        "position" : 0, 
        "categoryId" : "8",
        "extensionAttributes": {}


Example Response : This is the same response regardless of what category or product.

  "message": "Could not save product \"%1\" with position %2 to category %3",
  "parameters": [

The second is setting it under custom_attribute using the product repository in either new or update calls. Both give the same result.

When setting the custom_attribute like below regardless of what sku and category I get an error. If I leave out "category_ids" it works fine but defeats the purpose of using the API to manage product import and updates.

'custom_attributes' => array(
            array( 'attribute_code' => 'category_ids', 'value' => ["8"] ),
            array( 'attribute_code' => 'description', 'value' => 'Simple Description' ),
            array( 'attribute_code' => 'short_description', 'value' => 'Simple  Short Description' ),

Error being the following, which is the only error I get regardless of what combination of sku and category even if the sku is not made yet and I am creating a new product..

"{\"message\":\"URL key for specified store already exists.\"}"

You have to check below code for set product in category with position,

        $url=""; //your custom site url...

        //Below 9 is category id....
        $product_url=$url. "rest/V1/categories/9/products"; 
        //Authentication rest API magento2, get access token
        $ch = curl_init();
        $data = array("username" => $username, "password" => $password);
        $data_string = json_encode($data);

        $ch = curl_init($token_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))
        $token = curl_exec($ch);
        $adminToken=  json_decode($token);

        //9 is category id...
        $sampleProductData = array(
                    "sku" => "24-MB01",
                    "position" => 5,
                    "category_id" => "9",
                    "extension_attributes" => array()
        $categoryData = json_encode(array('productLink' => $sampleProductData));

        $setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $product_url);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $categoryData);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
  • hi rakesh, Please open once this url. and place order with cod. place order is not working. depoto.com its urgent to me. sorry for asking here – Teja Bhagavan Kollepara Jan 12 '17 at 12:29
  • Look at your array for adding to category and then look at mine, they are almost identical. categoryId and category_id as the key result in the same error. Setting a different position doesn't change that. – Tyler Jan 12 '17 at 14:29
  • You can just try with above solutions. I have test it. – Rakesh Jesadiya Jan 12 '17 at 14:44
  • This is the result of using your code : "{"message":"Could not save product \"%1\" with position %2 to category %3","parameters":["237",5,"17"]}" – Tyler Jan 12 '17 at 16:07
  • Have you checked category with id 9 exist or not for your admin? – Rakesh Jesadiya Jan 12 '17 at 16:09

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.