首页 > 解决方案 > Magento 2 自定义 REST API 多部分/表单数据

问题描述

我正在使用 Magento2 自定义 REST API。我需要上传一个.csv文件 vai API。我一直在寻找multipart/form-data在我的 REST API 中支持的解决方案。但不幸的是还没有找到合适的解决方案。帮助我分享有关如何支持Magento2 REST APImultipart/form-data的任何想法?

标签: phpapirestmagento2multipartform-data

解决方案


我希望我能帮助你。

您可以通过执行以下操作将响应类型添加到 magento REST API:

  • 创建 webapi 响应渲染器
    {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')
        );
    }
}

  • 将该渲染器添加到支持的 webapi 响应中
    {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>

根据您的项目更改 {VENDOR_NAME} 和 {ROOT} 即可。
如果您需要任何澄清,请随时询问。
编辑:
webapi.xml是非常基本的。要获得我们上面在响应中创建的响应类型,您必须将 Accept HTML Header(在 ajax 调用 fe 中)设置为multipart/form-data. Magento 2 API 响应始终通过检查此标头来呈现。
{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>

现在您只需在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>

请参阅有关创建自定义 API 路由/控制器的本教程。


推荐阅读