简体   繁体   中英

Magento 2 Custom REST API multipart/form-data

I'm working with Magento2 Custom REST APIs. I need to upload a .csv file vai API. I have been searching for a solution to support multipart/form-data in my REST API. But unfortunately haven't found proper solution. Help me sharing any idea on how I can support multipart/form-data in Magento2 REST APIs ?

I hope I will be able to help you.

You can add response types to magento REST API by doing the following:

  • Create a webapi response renderer
    {ROOT}\app\code\{VENDOR_NAME}\Webapi\Webapi\Rest\Response\Renderer
<?php


namespace {VENDOR_NAME}\Webapi\Webapi\Rest\Response\Renderer;

use Magento\Framework\Webapi\Rest\Response\RendererInterface;
use Magento\Framework\Webapi\Exception as WebApiException;

class Multipart implements RendererInterface
{
    /**
     * Renderer mime type.
     */
    const MIME_TYPE = 'multipart/form-data';

    /**
     * @return string
     */
    public function getMimeType() {
        return self::MIME_TYPE;
    }

    /**
     * @param object|array|int|string|bool|float|null $data
     * @return string
     * @throws WebApiException
     */
    public function render($data) {
        // return reponse according to your needs
        if (is_string($data)) {
            return $data;
        }
        throw new WebApiException(
            __('Internal Server Error')
        );
    }
}

  • Add that renderer to supported webapi responses
    {ROOT}\app\code\{VENDOR_NAME}\Webapi\etc\di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Webapi\Rest\Response\RendererFactory">
        <arguments>
            <argument name="renders" xsi:type="array">
                <item name="multipart" xsi:type="array">
                    <item name="type" xsi:type="string">multipart/form-data</item>
                    <item name="model" xsi:type="string">{VENDOR_NAME}\Webapi\Webapi\Rest\Response\Renderer\Multipart</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

Change {VENDOR_NAME} and {ROOT} according to your project and you are good to go.
If you need any clarification, feel free to ask.
EDIT:
The webapi.xml is very basic. To get the response type that we created above in respone, you will have to set the Accept HTML Header (in ajax call fe) to multipart/form-data . Magento 2 API Responses are always rendered by checking this header.
{ROOT}/app/code/{VENDOR_NAME}/Webapi/etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
    <route url="/V1/yourMethod" method="GET">
        <service class="{VENDOR_NAME}\Webapi\Api\{API_CLASS_NAME}Interface" method="yourMethod" />
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>
</routes>

Now you just have to set a prefered model for the interface in di.xml .
{ROOT}/app/code/{VENDOR_NAME}/Webapi/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
   <type name="Magento\Framework\Webapi\Rest\Response\RendererFactory">
        <arguments>
            <argument name="renders" xsi:type="array">
                <item name="multipart" xsi:type="array">
                    <item name="type" xsi:type="string">multipart/form-data</item>
                    <item name="model" xsi:type="string">{VENDOR_NAME}\Webapi\Webapi\Rest\Response\Renderer\Multipart</item>
                </item>
            </argument>
        </arguments>
    </type>
    <preference for="{VENDOR_NAME}\Webapi\Api\{API_CLASS_NAME}Interface"
                type="{VENDOR_NAME}\Webapi\Model\Api\{API_CLASS_NAME}" />
</config>

See this tutorial about creating custom API routes / controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM