php - Magento 2 自定义 REST API 多部分/表单数据
问题描述
我正在使用 Magento2 自定义 REST API。我需要上传一个.csv
文件 vai API。我一直在寻找multipart/form-data
在我的 REST API 中支持的解决方案。但不幸的是还没有找到合适的解决方案。帮助我分享有关如何支持Magento2 REST APImultipart/form-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 路由/控制器的本教程。
推荐阅读
- php - 在测试中注入依赖项
- sql - 更新 SQL Server 表以通过存储过程执行此操作
- javascript - 如何创建单个对象的数组Javascript
- excel - 变量不会随着每个循环而改变
- python - 如何通过单击 Ubuntu 的按钮重新启动 TeamViewer?
- java - 根据单元格的内容将新行插入 Apache POI 电子表格
- ios - 尝试后台刷新
- json - 如何将 JSON 加入表(SQL Server)
- android - 如何在kotlin中更改tomtom地图的标记
- typescript - Typescript 编译器选项 - 尝试将“平面”输出输出到 outDir