Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento2; update stock by REST API

Tags:

I've set up a local Magento 2.1.2 environment with sample data. I'm trying to find the correct code for updating stocks through the REST api (catalogInventoryStockRegistryV1 - Put).

This is what i've got so far:

<?php

$adminUrl = 'http://www.localurl.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "66sRz97CZt7N[GdqFU");

$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token=  json_decode($token);

$headers = array("Authorization: Bearer $token");

$requestUrl='http://www.localurl.pro/rest/V1/products/24-MB01/stockItems/1';
// Sample SKU

$sampleProductData = array(
        "qty" => 100
);
$productData = json_encode(array('stockItem' => $sampleProductData));
// Prints: {"stockItem":{"qty":100}}

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

This script gives the following error:

string(122) "{"message":"Server cannot understand Content-Type HTTP header media type application\/x-www-form-urlencoded","trace":null}"

I'm not sure what the message exactly means. Some help would be greatly appreciated.

Solution

Solution given by Mohan Gopal. 'Content-Type: application/json' was missing in the curl header.

<?php

$adminUrl = 'http://www.localhost.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "66sRz97CZt7N[GdqFU");

$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token=  json_decode($token);

//Use above token into header
$headers = array("Authorization: Bearer $token","Content-Type: application/json");

$skus = array(
  '24-MB04' => 10,
  '24-MB03' => 5
);

foreach ($skus as $sku => $stock) {
  $requestUrl='http://www.localurl.pro/rest/V1/products/' . $sku . '/stockItems/1';

  $sampleProductData = array(
          "qty" => $stock
  );
  $productData = json_encode(array('stockItem' => $sampleProductData));

  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $requestUrl);
  curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  curl_close($ch);
  var_dump($response);

  unset($productData);
  unset($sampleProductData);
}
like image 294
Tjitse Avatar asked Nov 22 '16 18:11

Tjitse


1 Answers

Add the content type to header and change the header from

$headers = array("Authorization: Bearer $token");

to

$headers = array("Authorization: Bearer $token","Content-Type: application/json");

after getting the token id on line 53.

like image 126
Mohan S Gopal Avatar answered Sep 23 '22 16:09

Mohan S Gopal