Magento – Rest API import product Magento2

magento-2.1magento2productrest api

I want to add a product using REST API but don't know how should I do.
could you please help me what should I do? Magento 2.1 I use. this is the code that i used:

{
    "id": 989898989989989898,
    "sku": "5",
    "name": "simple test",
    "attribute_set_id": 23,
    "price": 25000,
    "status": 1,
    "visibility": 4,
    "type_id": "simple",
    "created_at": "2019-07-02 11:00:15",
    "updated_at": "2019-07-02 11:50:25",
    "weight": 10,
    "extension_attributes": {
        "stock_item": {
            "item_id": 998989898989898,
            "product_id": 98989898989899898989898,
            "stock_id": 1,
            "qty": 5,
            "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": 1,
            "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": false,
            "manage_stock": true,
            "low_stock_date": null,
            "is_decimal_divided": false,
            "stock_status_changed_auto": 0
        }
    },
    "product_links": [],
    "options": [],
    "media_gallery_entries": [
        {
            "id": 6034,
            "media_type": "image",
            "label": null,
            "position": 1,
            "disabled": false,
            "types": [
                "small_image",
                "thumbnail",
                "swatch_image"
            ],
            "file": "/1/5/87987.jpg"
        },
        {
            "id": 6035,
            "media_type": "image",
            "label": null,
            "position": 2,
            "disabled": false,
            "types": [],
            "file": "/1/5/656565.jpg"
        }
    ],
    "tier_prices": [],
    "custom_attributes": [
        {
            "attribute_code": "description",
            "value": "<ul><li>des test</li></ul>"
        },
        {
            "attribute_code": "short_description",
            "value": "<ul><li>shhort desc</ul>"
        },
        {
            "attribute_code": "small_image",
            "value": "/1/5/1540017-5714-7100451-3.jpg"
        },
        {
            "attribute_code": "thumbnail",
            "value": "/1/5/1540017-5714-7100451-3.jpg"
        },
        {
            "attribute_code": "category_ids",
            "value": [
                "418",
                "419",
                "2256",
                "2257"
            ]
        },
        {
            "attribute_code": "options_container",
            "value": "container1"
        },
        {
            "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": "4"
        },
        {
            "attribute_code": "gift_message_available",
            "value": "0"
        },
        {
            "attribute_code": "tax_class_id",
            "value": "2"
        },
        {
            "attribute_code": "swatch_image",
            "value": "/1/5/1540017-5714-7100451-3.jpg"
        },
        {
            "attribute_code": "sm_featured",
            "value": "0"
        },
        {
            "attribute_code": "udropship_calculate_rates",
            "value": "0"
        },
        {
            "attribute_code": "udropship_vendor",
            "value": "5"
        },
        {
            "attribute_code": "udtiership_use_custom",
            "value": "1"
        },
        {
            "attribute_code": "udtiership_rates",
            "value": [
                {
                    "rate_id": "5",
                    "product_id": "4036",
                    "delivery_type_id": "13",
                    "customer_shipclass_id": "475",
                    "customer_group_id": "*",
                    "cost": "222000.0000",
                    "additional": "0.0000",
                    "handling": "0.0000",
                    "sort_order": "0"
                }
            ]
        },
    ]
}

but get below error

{
    "message": "%fieldName is a required field.",
    "parameters": {
        "fieldName": "product"
    }
}

on this URL

/rest/default/V1/products

Best Answer

Here you can create all types of products, I gave for simple product creation. You can check Magento swagger and You can change only input data js format

https://devdocs.magento.com/swagger/index_22.html

 <?php
   $url="http://xx.xx.xx.xx/magentoc23/index.php/";
  <!-- start code,here we are creating Authentication --> 
   $token_url=$url."rest/V1/integration/admin/token";
   $username="adminusername";
   $password="adminpassword";

   $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);
<!------------------- end code------------> 


  <!-- start code, here we are creating products and we can loop from here to --> 

   $product_data = '{"product": 
  {"sku":"sss11","name":"sss11","attribute_set_id":4,"price":60}}';
  //Here product_data we have to read data from csv file.If you need data format means check in swagger

   $setHaders = array('Content-Type:application/json','Authorization:Bearer 
    '.$adminToken);
    $product_url=$url. "rest/V1/products";  //This is endpoint for create all products, you have to change input Data.

  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $product_url);
  curl_setopt($ch,CURLOPT_POSTFIELDS, $product_data);
  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);
 curl_close($ch);
 var_dump($response);
<!----------- end code------------> 
Related Topic