0

I'm writing a Magento 2 client library in nodeJS and I've created some product attributes via this POST API /rest/V1/products/attributes which worked fine but I want to add update method as well, for which I tried this PUT API /rest/V1/products/attributes/{attributeCode} and it's not letting me update the product attributes as this always returns with error: Attribute with the same code already exists.

Yes it exists already, that's why I'm trying to update it. Following is the example request I'm making via Postman.

URL: https://MY_HOST/rest/V1/products/attributes/advantages

Body (JSON):

{
"attribute":{
  "frontend_input":"text",
  "attribute_code":"advantages",
  "default_frontend_label":"f max 1"
  }
}

It's been many hours now and I still can't figure out why it's not updating, it seems like it tries to insert a new product attribute as I tried to pass attribute code which does not exists in magento and it created a new attribute with that code, which doesn't seems right with PUT method. Any help in this regard would be highly appreciated.

Thanks & Regards.

1 Answer 1

1

Based on the code of the PUT API /rest/V1/products/attributes/{attributeCode} endpoint, it appears that the endpoint implementation requires specifying the attribute_id in the body of the request.

When making a PUT request to update an attribute, you need to provide both the attributeCode in the endpoint URL and the attribute_id in the request body. The attribute_id serves as a unique identifier for the attribute you want to update. Without this attribute_id, the API assumes that you want to create a new attribute instead of updating an existing one.

It's weird, but that's how it works. :-)

1
  • Thanks @JBrada . It's really weird but it works now :)
    – Asad ullah
    Aug 3, 2023 at 18:19

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.