3

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?

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

1
  • I am getting an error "Undefined variable responseArray" while trying the above solution, could you plz suggest where I should declare $responseArray Sep 1 at 17:51
1

This question pops up quite a bit but I've find no complete answer so far, so here goes:

Using this guide's example (Example 3: Mid-point Between two Points), you basically need to add two more things in order to return a simple JSON via REST API:

  1. a data structure interface (PointInterface.php in the guide) that has setters and getters for the properties you wish to include in the JSON. This interface is used as a return type annotation in the REST service contract (CalculatorInterface.php)
  2. a model that implements the above mentioned data structure interface (Point.php), which basically has private properties for the properties you wish to include in the JSON, and implements some basic setters/getters on them

After implementing 1) and 2), all that's left is to use a factory to create the new model, set its properties and just return it in the REST API method (no json_encode, print_r, etc required) as seen here in the Calculator.php class:

/**
 * Compute mid-point between two points.
 *
 * @api
 * @param PointInterface $point1 The first point.
 * @param PointInterface $point2 The second point.
 * @return PointInterface The mid-point.
 */
public function midPoint($point1, $point2) {
    $point = $this->pointFactory->create();
    $point->setX(($point1->getX() + $point2->getX()) / 2.0);
    $point->setY(($point1->getY() + $point2->getY()) / 2.0);
    return $point;
}
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;
   }

  }
6
  • this will truncate dsal node and give ouput like [ { "estatus": [ { "codigo": 400, "mensaje": "my message" } ] } ] Feb 13 '19 at 12:02
  • return $this->response[0]; Feb 13 '19 at 12:11
  • found the problem? Feb 13 '19 at 12:23
  • Not yet $this->response[0]; not working Feb 13 '19 at 12:26
  • $this->response = ['dsal' => ['estatus' => ['0' => ['codigo' => 400, 'mensaje' => 'my message']]]]; Feb 13 '19 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.