2

I have developed my custom rest API endpoint. Here is the code

webapi.xml

<?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/createcustomorder">
    <service class="Demo\CreateOrderApi\Api\CreateCustomOrderManagementInterface" method="postCreateCustomOrder"/>
    <resources>
        <resource ref="Magento_Sales::create"/>
    </resources>
</route>

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">
  <preference for="Demo\CreateOrderApi\Api\CreateCustomOrderManagementInterface" type="Demo\CreateOrderApi\Model\CreateCustomOrderManagement"/>
</config>

Api/CreateCustomOrderManagementInterface.php

<?php
namespace Demo\CreateOrderApi\Api;

interface CreateCustomOrderManagementInterface
{

/**
 *
 *
 * @param array $orderData[]
 * @return mixed[]
 *
 *
 */
public function postCreateCustomOrder();
}

Model/CreateCustomOrderManagement.php

 <?php
 namespace Demo\CreateOrderApi\Model;

  class CreateCustomOrderManagement implements \Demo\CreateOrderApi\Api\CreateCustomOrderManagementInterface
  {

   /**
   * @return mixed[]
   */
   public function postCreateCustomOrder()
   {
     /**
     --rest of code--
     **/

     $this->response[]['dsal'] = ['estatus' => ['0' => ['codigo' => 400, 'mensaje' => 'my message']]];

   return $this->response;
   }

  } 

In my rest client, I'm expecting output like this

 {
    "dsal": {
        "estatus": [
            {
                "codigo": 400,
                "mensaje": "No such entity with customerId = 12"
            }
        ]
    }
 }

but Magento gives me output with extra braces

[
 {
    "dsal": {
        "estatus": [
            {
                "codigo": 400,
                "mensaje": "No such entity with customerId = 12"
            }
        ]
    }
 }

]   

Can anybody tell me what wrong with return type and return array in my code?

  • try putting parenthesis be estatus like this $this->response[]['dsal'] =( ['estatus' => ['0' => ['codigo' => 400, 'mensaje' => 'my message']]]); – fmsthird Feb 13 at 11:42
  • try this $res = $this->response; print_r(json_encode($res)); – fmsthird Feb 13 at 12:30
  • 1
    @magefms $res = $this->response; return print_r(json_encode($res)); seems working but is this correct way to return? – chirag dodia Feb 13 at 12:40
  • yeah it's the same , you just encode the return value into json format using json_encode. – fmsthird Feb 13 at 12:43
  • @magefms I'm finding another solution as print_r should not be used in return. This is not a standard way – chirag dodia Feb 15 at 10:17
1

I had the same problem, and finally I did this:

<?php
 namespace Demo\CreateOrderApi\Model;

  class CreateCustomOrderManagement implements \Demo\CreateOrderApi\Api\CreateCustomOrderManagementInterface
  {

   /**
   * @return mixed[]
   */
   public function postCreateCustomOrder()
   {
     /**
     --rest of code--
     **/
       header("Content-Type: application/json; charset=utf-8");
       $this->response = json_encode($responseArray);
       print_r($this->response,false);
       die();
   }
  }

It is not the best solution but it's the only way it works.

0

try this

     <?php
 namespace Demo\CreateOrderApi\Model;

  class CreateCustomOrderManagement implements \Demo\CreateOrderApi\Api\CreateCustomOrderManagementInterface
  {

   /**
   * @return mixed[]
   */
   public function postCreateCustomOrder()
   {
     /**
     --rest of code--
     **/

     $this->response['dsal'] = ['estatus' => ['0' => ['codigo' => 400, 'mensaje' => 'my message']]];

   return $this->response;
   }

  }
  • this will truncate dsal node and give ouput like [ { "estatus": [ { "codigo": 400, "mensaje": "my message" } ] } ] – chirag dodia Feb 13 at 12:02
  • return $this->response[0]; – Serhii Havrylchuk Feb 13 at 12:11
  • found the problem? – Serhii Havrylchuk Feb 13 at 12:23
  • Not yet $this->response[0]; not working – chirag dodia Feb 13 at 12:26
  • $this->response = ['dsal' => ['estatus' => ['0' => ['codigo' => 400, 'mensaje' => 'my message']]]]; – Serhii Havrylchuk Feb 13 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.