0

When i am using Content-Type: "application/x-www-form-urlencoded" in API i getting the error:

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

With Content-Type: "application/json" this works, but is necessary too with application/x-www-form-urlencoded.

I add deserializer in app/etc/di.xml

<type name="Magento\Framework\Webapi\Rest\Request\DeserializerFactory">
        <arguments>
            <argument name="deserializers" xsi:type="array">
                <item name="application_json" xsi:type="array">
                    <item name="type" xsi:type="string">application/json</item>
                    <item name="model" xsi:type="string">Magento\Framework\Webapi\Rest\Request\Deserializer\Json</item>
                </item>
                <item name="application_xml" xsi:type="array">
                    <item name="type" xsi:type="string">application/xml</item>
                    <item name="model" xsi:type="string">Magento\Framework\Webapi\Rest\Request\Deserializer\Xml</item>
                </item>
                <item name="application_xhtml_xml" xsi:type="array">
                    <item name="type" xsi:type="string">application/xhtml+xml</item>
                    <item name="model" xsi:type="string">Magento\Framework\Webapi\Rest\Request\Deserializer\Xml</item>
                </item>
                <item name="text_xml" xsi:type="array">
                    <item name="type" xsi:type="string">text/xml</item>
                    <item name="model" xsi:type="string">Magento\Framework\Webapi\Rest\Request\Deserializer\Xml</item>
                </item>
                <item name="application_x_www_form_urlencoded" xsi:type="array">
                    <item name="type" xsi:type="string">application/x-www-form-urlencoded</item>
                    <item name="model" xsi:type="string">Magento\Framework\Webapi\Rest\Request\Deserializer\Json</item>
                </item>
            </argument>
        </arguments>
    </type>

And get this response:

{
  "message": "The \"string\" value's type is invalid. The \"string[]\" type was expected. Verify and try again.",
  "trace": null
}

This is possible? And why Magento 2 not support application/x-www-form-urlencoded?

Thanks!

1 Answer 1

0

you need to create a separate file for content type urlencoded

<?php
/**
 * FormUrlencoded deserializer of REST request content.
 */
namespace Vendor\Module\Webapi\Rest\Request\Deserializer;

use InvalidArgumentException;
use Magento\Framework\App\State;
use Magento\Framework\Phrase;
use Magento\Framework\Stdlib\Parameters;
use Magento\Framework\Webapi\Rest\Request\DeserializerInterface;

/**
 * Class FormUrlencoded
 */
class FormUrlencoded implements DeserializerInterface
{
    /**
     * @var Parameters
     */
    private $parameters;
    /**
     * @var State
     */
    protected $_appState;
    /**
     * FormUrlencoded constructor.
     * @param Parameters $parameters
     * @param State $appState
     */
    public function __construct(
        Parameters $parameters,
        State $appState
    ) {
        $this->parameters = $parameters;
        $this->_appState = $appState;
    }

    /**
     * Parse Request body into array of params.
     *
     * @param string $encodedBody Posted content from request.
     * @return array|null Return NULL if content is invalid.
     * @throws InvalidArgumentException
     * @throws \Magento\Framework\Webapi\Exception If decoding error was encountered.
     */
    public function deserialize($encodedBody)
    {
        if (!is_string($encodedBody)) {
            throw new \InvalidArgumentException(
                sprintf('"%s" data type is invalid. String is expected.', gettype($encodedBody))
            );
        }
        try {
            $decodedBody = urldecode($encodedBody);
            $this->parameters->fromString($decodedBody);
            return $this->parameters->toArray();
        } catch (\InvalidArgumentException $e) {
            if ($this->_appState->getMode() !== State::MODE_DEVELOPER) {
                throw new \Magento\Framework\Webapi\Exception(new Phrase('Decoding error.'));
            } else {
                throw new \Magento\Framework\Webapi\Exception(
                    new Phrase(
                        'Decoding error: %1%2%3%4',
                        [PHP_EOL, $e->getMessage(), PHP_EOL, $e->getTraceAsString()]
                    )
                );
            }
        }
    }
}

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.