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": [
"144",
0,
"8"
]
}
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.\"}"
Best Answer
You have to check below code for set product in category with position,