Hello Magento Friends,
In this Magento 2 tutorial guide, I will show How to Pass a Multi-Dimensional Array as an Argument in REST API in Magento 2.
A multi-dimensional array is an array that contains one or more arrays as its elements. In other words, each element of a multi-dimensional array can itself be an array. REST API allows you to interact with Magento programmatically.
However, there are instances where you might need to pass multi-dimensional arrays as arguments in your REST API calls, and this can be a bit tricky. In this blog post, we’ll explore how to do just that.
Steps to Pass Multi-Dimensional Array as an Argument in REST API in Magento 2:
Step 1: First, we need to create a “webapi.xml“ file inside our extension at the following path
app\code\Vendor\Extension\etc\webapi.xml
And add the code as follows
<?xml version="1.0" ?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> <route method="POST" url="/V1/vender-extension/submitform"> <service class="Vendor\Extension\Api\PostManagementInterface" method="setFormData"/> <resources> <resource ref="anonymous"/> </resources> <data> <parameter name="orderarray" force="true">%orderarray%</parameter> </data> </route> </routes>
Step 2: Now, we need to create a “di.xml“ file inside our extension at the following path
app\code\Vendor\Extension\etc\di.xml
Then include the following piece of code
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Vendor\Extension\Api\PostManagementInterface" type="Vendor\Extension\Model\PostManagement" /> </config>
Step 3: Now, we need to create a “PostManagementInterface.php“ file inside our extension at the following path
app/code/Vendor/Extension/Api/PostManagementInterface.php
And add the code as mentioned below
<?php namespace Vendor\Extension\Api; interface PostManagementInterface { /** * SET form data * @param mixed $orderarray * @return mixed */ public function setFormData($orderarray); }
Step 4: Now, we need to create a “PostManagementInterface.php“ file inside our extension at the following path
app/code/Vendor/Extension/Model/PostManagement.php
After that, add the following code
<?php namespace Vendor\Extension\Model; use Vendor\Extension\Api\PostManagementInterface; class PostManagement implements PostManagementInterface { public function setFormData($orderarray) { $data = []; $requestData = json_decode(file_get_contents('php://input'), true); $getOrderArray = $requestData['orderarray']; print_r($getOrderArray); $data = ["status"=>true, "message"=> "passes data successfully."]; return [$data]; } }
You need to call above API as per below parameters.
{ "orderarray": [ {"orderId": "1", "comment": "test order"}, {"orderId": "2", "comment": "demo order"} ] }
Conclusion:
Hope you understood How to Pass Multi-Dimensional Array as an Argument in REST API in Magento 2. If you have any doubt, let me know through the comment section and I will quickly get back to you with the solution. Alternatively, Hire a Magento Developer for the custom requirements of your Magento 2 store. Share the tutorial with your friends and stay connected with us for more such Magento 2 solutions.
Happy Coding!