2

I am struggling because I get an invalid response from my custom API module for Magento 2 (2.2.5). When I send a request to my endpoint via GET with the header "Accept application/xml" (which is totally valid for Magento 2) I get my XML reponse. However, the surrounding <response> tag pops up twice and I can't get rid of the second one. Here is a sample result:

<response>
    <test>value</test>
    <response/>
</response>

Problem here is the <response/> in line 3. Where does this come from/how can I get rid of this?

I'll only point out the most important files since the rest is just bare standard. For testing purposes I set the resource to anonymous (it's local so no worries here).

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 url="/V1/xmlorderexport/get" method="GET">
        <service class="Vendor\XmlOrderExportApi\Api\XmlInterface" method="get"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Api/XmlInterface.php:

<?php

namespace Vendor\XmlOrderExportApi\Api;

interface XmlInterface
{

    /**
     * Returns order data as XML.
     *
     * @api
     * @return string Returns the order as XML based on the given template.
     */
    public function get();
}

Model/Xml.php:

<?php

namespace Vendor\XmlOrderExportApi\Model;

use \Vendor\XmlOrderExportApi\Api\XmlInterface;
use \Vendor\XmlOrderExport\Model\WebapiInterface;
use \Magento\Framework\Webapi\Rest\Response\Renderer\Xml as Renderer;

class Xml implements XmlInterface
{

    private $webapi;

    private $renderer;


    function __construct(
        WebapiInterface $webapi,
        Renderer $renderer
    )
    {
        $this->renderer = $renderer;
        $this->webapi   = $webapi;
    }

    /**
     * Returns order data as XML.
     *
     * @api
     * @return string Returns the order as XML based on the given template.
     */
    public function get()
    {
        /**
         * Only for testing purposes!
         */
        //$order_id = 194;

        //$result = $this->webapi->getXml($order_id);
        //$xml    = simplexml_load_string($result);

        $this->renderer->render(['test' => 'value']);
    }
}

Can anybody help me out and point me in the right direction please?

Thanks in advance ...

Your Answer

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

Browse other questions tagged or ask your own question.