Skip to content

Error in Postman and show that Invalid type for value: “%productId%

I have developed a module in my magento 2.2.6 for to create an API that can update the product price and quantity via product id . Please see my code below

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/registration.php

<?php

MagentoFrameworkComponentComponentRegistrar::register(

    MagentoFrameworkComponentComponentRegistrar::MODULE,

    'AdmNetsuitePQ_NetsuitePQ',

    __DIR__

);

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/Model/ProductUpdate.php

<?php
namespace AdmNetsuitePQNetsuitePQModel;

use MagentoFrameworkExceptionInputException;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkWebapiException as HTTPExceptionCodes;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoCatalogModelProductFactory;
use AdmNetsuitePQNetsuitePQApiProductUpdateInterface;

class ProductUpdate implements ProductUpdateInterface
{
    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function update($productId, $price = null, $quantity = null)
    {
        $productId = (int) $productId;
        $product = $this->productRepository->getById($productId);
        if (!$product->getId()) {
            throw new NoSuchEntityException(__('Product with ID %1 not found.', $productId));
        }
        if ($price !== null && $price < 0) {
            throw new InputException(__('Price must be greater than or equal to 0.'));
        }
        if ($quantity !== null && $quantity < 0) {
            throw new InputException(__('Quantity must be greater than or equal to 0.'));
        }
        if ($price !== null) {
            $product->setPrice($price);
        }
        if ($quantity !== null) {
            $product->setQty($quantity);
            $product->setIsQtyDecimal(false);
        }
        $this->productRepository->save($product);
        return true;
    }
}

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/Api/ProductUpdateInterface.php

<?php
namespace AdmNetsuitePQNetsuitePQApi;

interface ProductUpdateInterface
{
    /**
     * Update product quantity and price
     *
     * @param int $productId
     * @param float|null $price
     * @param float|null $quantity
     * @return bool
     * @throws MagentoFrameworkExceptionNoSuchEntityException
     * @throws MagentoFrameworkExceptionInputException
     * @throws MagentoFrameworkExceptionStateException
     * @throws MagentoFrameworkExceptionLocalizedException
     */
    public function update($productId, $price = null, $quantity = null);
}

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

    <module name="AdmNetsuitePQ_NetsuitePQ" setup_version="1.0.0"> </module>

</config>

/var/www/html/app/code/AdmNetsuitePQ/NetsuitePQ/etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/product/update" method="POST">
        <service class="AdmNetsuitePQNetsuitePQApiProductUpdateInterface" method="update"/>
        <resources>
            <resource ref="Magento_Catalog::products"/>
        </resources>
        <data>
            <parameter name="productId" force="true">%productId%</parameter>
            <parameter name="price" force="false">%price%</parameter>
            <parameter name="quantity" force="false">%quantity%</parameter>
        </data>
    </route>
</routes>

Now when I am sending data via postman

POST /rest/V1/product/update HTTP/1.1
Host: my_site.com
Content-Type: application/json
Authorization: OAuth oauth_consumer_key="j9q7uilt4trla8hsi5lo1gtbtuh46icr",oauth_token="df4lz1gasp0su25rbwnp225kxfwknehn",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1678733662",oauth_nonce="kfxspsm3a2n",oauth_version="1.0",oauth_signature="sdKx5cO%2BU7fePuKPWfgZy5ZCAks%3D"
Cookie: PHPSESSID=23vl60opom23c1gfqalnh914e9
Content-Length: 65

{
    "productId": 122,
    "price": 0,
    "quantity": 12
}

{ “message”: “Invalid type for value: “%productId%”. Expected Type: “int”.” }

Please help to solve this error. Please check my module code is correct or not.