Magento2; update stock by REST API
<p>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). </p>

<p>This is what i've got so far:</p>

<pre><code>&lt;?php

$adminUrl = 'http://www.localurl.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" =&gt; "admin", "password" =&gt; "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" =&gt; 100
);
$productData = json_encode(array('stockItem' =&gt; $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);
</code></pre>

<p>This script gives the following error:</p>

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

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

<h2>Solution</h2>

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

<pre><code>&lt;?php

$adminUrl = 'http://www.localhost.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" =&gt; "admin", "password" =&gt; "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' =&gt; 10,
  '24-MB03' =&gt; 5
);

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

  $sampleProductData = array(
          "qty" =&gt; $stock
  );
  $productData = json_encode(array('stockItem' =&gt; $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);
}
</code></pre>