cancel
Showing results for 
Search instead for 
Did you mean: 

Can't Update exisiting product with REST call ( PUT )

Can't Update exisiting product with REST call ( PUT )

Hi There,

 

I've got a serious issue, I'm trying to update the name of my products using this REST call

 

/V1/products/sku . 

 

Now the problem is, I only want to update my name, but I need to include the SKU in the json string. Now what this does is it cant save my product because he thinks it's a new product, but the SKU already exists. How do I update my product name trough the REST api then?

 

EDIT: Added images of code and error.

 

PS1.PNG

 

ps2.PNG

 

Regards

2 REPLIES 2

Re: Can't Update exisiting product with REST call ( PUT )

first i suggest you use some extension to have a more readable format of your error ( on chrome JSONView) 

second i see that for name you're using single quotes instead of double

i've tested with similar params and is working on a magento 2 clean installation with sample data

 

{
  "product": {
    "sku": "MH07-M-Green",
    "name": "Hero Hoodie-M-Green new",
    "attributeSetId": 9
  },
  "saveOptions": true
}

 

here is the code for the request :

  

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "http://m2-playground.dev/rest/V1/products/MH07-M-Green",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\n \"product\": {\n \"sku\": \"MH07-M-Green\",\n \"name\": \"Hero Hoodie-M-Green new\",\n \"attributeSetId\": 9\n },\n \"saveOptions\": true\n}",
CURLOPT_HTTPHEADER => array(
"authorization: OAuth oauth_consumer_key=\"33mun8ejynbmsii298tcd1r1g521k6gp\",oauth_token=\"vmm8mmo0oc52ysgbg68wqp22qtmp4hbn\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1466211088\",oauth_nonce=\"LUBaL2\",oauth_version=\"1.0\",oauth_signature=\"WQrhwzcV4wGr67S%2FWX7Z2usjvLM%3D\"",
"cache-control: no-cache",
"content-type: application/json",
"postman-token: 096a27cc-4727-3766-0ed2-055f2ca4bd42"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

and the correct answer expected:

 

 

{
  "id": 156,
  "sku": "MH07-M-Green",
  "name": "Hero Hoodie-M-Green new",
  "attribute_set_id": 9,
  "price": 54,
  "status": 1,
  "visibility": 1,
  "type_id": "simple",
  "created_at": "2016-06-17 11:11:51",
  "updated_at": "2016-06-18 00:51:28",
  "weight": 1,
  "extension_attributes": {
    "stock_item": {
      "item_id": 156,
      "product_id": 156,
      "stock_id": 1,
      "qty": 99,
      "is_in_stock": true,
      "is_qty_decimal": false,
      "show_default_notification_message": false,
      "use_config_min_qty": true,
      "min_qty": 0,
      "use_config_min_sale_qty": 0,
      "min_sale_qty": 1,
      "use_config_max_sale_qty": true,
      "max_sale_qty": 10000,
      "use_config_backorders": true,
      "backorders": 0,
      "use_config_notify_stock_qty": true,
      "notify_stock_qty": 1,
      "use_config_qty_increments": true,
      "qty_increments": 0,
      "use_config_enable_qty_inc": true,
      "enable_qty_increments": false,
      "use_config_manage_stock": true,
      "manage_stock": true,
      "low_stock_date": null,
      "is_decimal_divided": false,
      "stock_status_changed_auto": 0
    }
  },
  "product_links": [],
  "options": [],
  "media_gallery_entries": [
    {
      "id": 240,
      "media_type": "image",
      "label": "",
      "position": 1,
      "disabled": false,
      "types": [
        "image",
        "small_image",
        "thumbnail"
      ],
      "file": "/m/h/mh07-green_main.jpg"
    }
  ],
  "tier_prices": [],
  "custom_attributes": [
    {
      "attribute_code": "description",
      "value": "<p>Gray and black color blocking sets you apart as the Hero Hoodie keeps you warm on the bus, campus or cold mean streets. Slanted outsize front pockets keep your style real . . . convenient.</p>\n<p>&bull; Full-zip gray and black hoodie.<br />&bull; Ribbed hem.<br />&bull; Standard fit.<br />&bull; Drawcord hood cinch.<br />&bull; Water-resistant coating.</p>"
    },
    {
      "attribute_code": "image",
      "value": "/m/h/mh07-green_main.jpg"
    },
    {
      "attribute_code": "small_image",
      "value": "/m/h/mh07-green_main.jpg"
    },
    {
      "attribute_code": "thumbnail",
      "value": "/m/h/mh07-green_main.jpg"
    },
    {
      "attribute_code": "color",
      "value": "53"
    },
    {
      "attribute_code": "category_ids",
      "value": []
    },
    {
      "attribute_code": "options_container",
      "value": "container2"
    },
    {
      "attribute_code": "required_options",
      "value": "0"
    },
    {
      "attribute_code": "has_options",
      "value": "0"
    },
    {
      "attribute_code": "msrp_display_actual_price_type",
      "value": "0"
    },
    {
      "attribute_code": "url_key",
      "value": "hero-hoodie-m-green"
    },
    {
      "attribute_code": "tax_class_id",
      "value": "2"
    },
    {
      "attribute_code": "size",
      "value": "169"
    }
  ]
}

Re: Can't Update exisiting product with REST call ( PUT )

Hi springerin,

 

unfortunately, it doesn't work for us. We have Magento 2.1.7 and only 

rest/all/V1/products

works. But it is setting all the websites for the product, even if the website_id is sent.

 

https://github.com/magento/magento2/issues/4952

It is the serious issue, I think...

 

Regards, Ivan