Shadowbob
Shadowbob

Reputation: 1430

Magento update inventory with REST

I Followed the instruction of this page http://www.magentocommerce.com/api/rest/Resources/inventory.html#RESTAPI-Resource-StockItems-HTTPMethod-PUT-stockitems--id and I have this code:

$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = $apiUrl.'/stockitems/429';
$productData = json_encode(array(
        'qty'           => 982,
        'is_in_stock'   => 1
    ));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
print_r($oauthClient->getLastResponseInfo());

But I get the return

[message:protected] => Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)

I tried with XML like this:

$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = $apiUrl.'/stockitems/429';
$productData = '<?xml version="1.0"?>
    <magento_api>
        <qty>99</qty>
    </magento_api>';
$headers = array('Content-Type' => 'text/xml');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
print_r($oauthClient->getLastResponseInfo());

and I get the error:

[message:protected] => Invalid auth/bad request (got a 405, expected HTTP/1.1 20X or a redirect)

I don't know what I'm doing wrong?
Thank you for your reply.

Upvotes: 1

Views: 2736

Answers (3)

Aleksey Razbakov
Aleksey Razbakov

Reputation: 634

Make sure you assigned to your admin user REST role.

Configuring Permissions Step By Step:

  • System -> Web Services -> REST - Roles -> Create new role
  • System -> Web Services -> REST - Consumers -> Create a Consumer -> Copy Key & Secret to use in your App
  • System -> Permissions -> Your User -> REST Role -> Select and Save your Role

Upvotes: 0

dangig
dangig

Reputation: 189

Another way to update item's quantity is to call the /products/:id API, method = PUT, with the following payload:

{"stock_data" : {"qty" : "123"}}

Upvotes: 1

Simon Zambrovski
Simon Zambrovski

Reputation: 756

You are using POST instead of PUT. HTTP Post is used for creating items in Magento, but if you update an item (or inventory, image, whatever) by id you have to use PUT.

Upvotes: 0

Related Questions