8

I construct a REST api using POST as follow

etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/module-webapi/etc/webapi.xsd">
    <route url="/V1/BonusPoint/updateFreeBonusPoint" method="POST">
        <service class="Vendor\BonusPoint\Api\BonusPointInterface" method="updateFreeBonusPoint"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Vendor\BonusPoint\Api\BonusPointInterface.php

<?php

namespace Vendor\BonusPoint\Api;

interface BonusPointInterface {

    /**
     * Update free bonus point of a customer.
     *
     * @api
     * @param int $customerId Customer ID.
     * @param int $point Free bonus point balance.
     * @return boolean True if success. Otherwise, false.
     */
    public function updateFreeBonusPoint($customerId, $point);
}

Vendor\BonusPoint\Model\BonusPoint.php

<?php

namespace Vendor\BonusPoint\Model;

use Vendor\BonusPoint\Api\BonusPointInterface;

class BonusPoint implements BonusPointInterface {

    protected $_customerBonusPointFactory;

    public function __construct(\Vendor\BonusPoint\Model\CustomerBonusPointFactory $customerBonusPointFactory) {
        $this->_customerBonusPointFactory = $customerBonusPointFactory;
    }

    public function updateFreeBonusPoint($customerId, $point) {
        try {
            $customerBonusPoint = $this->_customerBonusPointFactory->create();
            $customerBonusPoint->load($customerId);
            $customerBonusPoint->setFreeBonusPoint($point);
            $customerBonusPoint->save();
            return true;
        } catch (Exception $ex) {
            return false;
        }
    }

}

When I run curl to test:

curl -X POST "http://local.magento/index.php/rest/V1/BonusPoint/updateFreeBonusPoint" \
     -d "customerId=1&point=123" \
     -H "Authorization: Bearer soqsupye97th0sd0le136u3l933vto1o" 

It returns:

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

What is the problem?

3 Answers 3

8

Magento do not support urlencoded requests.

Please specify content type: Add -H "Content-type: application/json" to curl command and send body as {"customerId": 1, "point":1 23}

3

I faced the same issue implementing a payment gateway. You need to define a way for Magento to deserialize url-encoded requests. Take a look to this repository. In more specific how Deserializer and Decoder works.

repo : https://github.com/ICEPAY/Magento-2

What you need :

https://github.com/ICEPAY/Magento-2/tree/master/Model/PostbackNotification https://github.com/ICEPAY/Magento-2/tree/master/Webapi/Rest/Request/Deserializer https://github.com/ICEPAY/Magento-2/blob/master/etc/di.xml

Similar issue : Magento 2 API: 3rd Party makes a post request to magento API with x-www-form-encoded data

0

Magento REST API expects only json encoded data (not url encoded data). Here is an example, show proper encoding of data and header.

<?php

$jsonEncodedData = json_encode(array('username'=>'user', 'password' => 'password'));

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://magento.local/rest/default/V1/integration/admin/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $jsonEncodedData,
  CURLOPT_HTTPHEADER => array(
    'Content-type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);

echo $response;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.