Question about API's and Postman and or anyway to retrieve data and populate parameters

Viewed 162

Not sure if this is possible but here's what i'm trying to accomplish through postman but i'm open to using terminal or anything recommended if it's easier.

I have this xmlSoap file and let's say it resides here: http://test.myapi.local:7947/my/path/here/myWSAPI

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="urn:microsoft-dynamics-schemas/codeunit/myWSAPI"
             targetNamespace="urn:microsoft-dynamics-schemas/codeunit/myWSAPI">
    <types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:microsoft-dynamics-nav/xmlports/x00000"
                elementFormDefault="qualified" targetNamespace="urn:microsoft-dynamics-nav/xmlports/x00000">
            <complexType name="APIDataCapture">
                <sequence>
                    <element minOccurs="1" maxOccurs="1" name="Type" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Code" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Qty1" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Qty2" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Qty3" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="Qty4" type="string"/>
                </sequence>
            </complexType>
            <complexType name="Root" mixed="true">
                <sequence>
                    <element minOccurs="1" maxOccurs="unbounded" name="APIDataCapture" type="tns:APIDataCapture"/>
                </sequence>
            </complexType>
            <element name="Root" type="tns:Root"/>
        </schema>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:microsoft-dynamics-nav/xmlports/x50014"
                elementFormDefault="qualified" targetNamespace="urn:microsoft-dynamics-nav/xmlports/x50014">
            <complexType name="APIDataCapture">
                <sequence>
                    <element minOccurs="1" maxOccurs="1" name="Type" type="string"/>
                    <element minOccurs="1" maxOccurs="1" default="0" name="EntryNo" type="int"/>
                    <element minOccurs="1" maxOccurs="1" name="SalesOrderFound" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="SalesOrderNo" type="string"/>
                    <element minOccurs="1" maxOccurs="1" default="0" name="SalesOrderLineNo" type="int"/>
                    <element minOccurs="1" maxOccurs="1" name="SalesOrderETADate" type="string"/>
                    <element minOccurs="1" maxOccurs="1" name="SalesOrderItemNo" type="string"/>
                </sequence>
            </complexType>
            <complexType name="Root" mixed="true">
                <sequence>
                    <element minOccurs="1" maxOccurs="unbounded" name="APIDataCapture" type="tns:APIDataCapture"/>
                </sequence>
            </complexType>
            <element name="Root" type="tns:Root"/>
        </schema>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
                targetNamespace="urn:microsoft-dynamics-schemas/codeunit/MyWSAPI">
            <element name="Get_Item_ETA_Date">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="itemNo" type="string"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Get_Item_ETA_Date_Result">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="return_value" type="date"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Find_Duplicate_Ext_Doc_No">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="custNo" type="string"/>
                        <element minOccurs="1" maxOccurs="1" name="extDocNo" type="string"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Find_Duplicate_Ext_Doc_No_Result">
                <complexType>
                    <sequence>
                        <element minOccurs="1" maxOccurs="1" name="return_value" type="boolean"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Get_Item_Quantity">
                <complexType>
                    <sequence>
                        <element xmlns:q1="urn:microsoft-dynamics-nav/xmlports/x00000" minOccurs="1" maxOccurs="1"
                                 name="aPIDataCapture" type="q1:Root"/>
                    </sequence>
                </complexType>
            </element>
            <element name="Get_Item_Quantity_Result">
                <complexType>
                    <sequence>
                        <element xmlns:q2="urn:microsoft-dynamics-nav/xmlports/x00000" minOccurs="1" maxOccurs="1"
                                 name="aPIDataCapture" type="q2:Root"/>
                    </sequence>
                </complexType>
            </element>
            <element name="GetSalesOrderETA">
                <complexType>
                    <sequence>
                        <element xmlns:q3="urn:microsoft-dynamics-nav/xmlports/x50014" minOccurs="1" maxOccurs="1"
                                 name="aPIDataCapture" type="q3:Root"/>
                    </sequence>
                </complexType>
            </element>
            <element name="GetSalesOrderETA_Result">
                <complexType>
                    <sequence>
                        <element xmlns:q4="urn:microsoft-dynamics-nav/xmlports/x50014" minOccurs="1" maxOccurs="1"
                                 name="aPIDataCapture" type="q4:Root"/>
                    </sequence>
                </complexType>
            </element>
        </schema>
    </types>
    <message name="Get_Item_ETA_Date">
        <part name="parameters" element="tns:Get_Item_ETA_Date"/>
    </message>
    <message name="Get_Item_ETA_Date_Result">
        <part name="parameters" element="tns:Get_Item_ETA_Date_Result"/>
    </message>
    <message name="Find_Duplicate_Ext_Doc_No">
        <part name="parameters" element="tns:Find_Duplicate_Ext_Doc_No"/>
    </message>
    <message name="Find_Duplicate_Ext_Doc_No_Result">
        <part name="parameters" element="tns:Find_Duplicate_Ext_Doc_No_Result"/>
    </message>
    <message name="Get_Item_Quantity">
        <part name="parameters" element="tns:Get_Item_Quantity"/>
    </message>
    <message name="Get_Item_Quantity_Result">
        <part name="parameters" element="tns:Get_Item_Quantity_Result"/>
    </message>
    <message name="GetSalesOrderETA">
        <part name="parameters" element="tns:GetSalesOrderETA"/>
    </message>
    <message name="GetSalesOrderETA_Result">
        <part name="parameters" element="tns:GetSalesOrderETA_Result"/>
    </message>
    <portType name="MyWSAPI_Port">
        <operation name="Get_Item_ETA_Date">
            <input name="Get_Item_ETA_Date" message="tns:Get_Item_ETA_Date"/>
            <output name="Get_Item_ETA_Date_Result" message="tns:Get_Item_ETA_Date_Result"/>
        </operation>
        <operation name="Find_Duplicate_Ext_Doc_No">
            <input name="Find_Duplicate_Ext_Doc_No" message="tns:Find_Duplicate_Ext_Doc_No"/>
            <output name="Find_Duplicate_Ext_Doc_No_Result" message="tns:Find_Duplicate_Ext_Doc_No_Result"/>
        </operation>
        <operation name="Get_Item_Quantity">
            <input name="Get_Item_Quantity" message="tns:Get_Item_Quantity"/>
            <output name="Get_Item_Quantity_Result" message="tns:Get_Item_Quantity_Result"/>
        </operation>
        <operation name="GetSalesOrderETA">
            <input name="GetSalesOrderETA" message="tns:GetSalesOrderETA"/>
            <output name="GetSalesOrderETA_Result" message="tns:GetSalesOrderETA_Result"/>
        </operation>
    </portType>
    <binding name="MyWSAPI_Binding" type="tns:MyWSAPI_Port">
        <binding xmlns="http://schemas.xmlsoap.org/wsdl/soap/" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="Get_Item_ETA_Date">
            <operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                       soapAction="urn:microsoft-dynamics-schemas/codeunit/MyWSAPI:Get_Item_ETA_Date" style="document"/>
            <input name="Get_Item_ETA_Date">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </input>
            <output name="Get_Item_ETA_Date_Result">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </output>
        </operation>
        <operation name="Find_Duplicate_Ext_Doc_No">
            <operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                       soapAction="urn:microsoft-dynamics-schemas/codeunit/myWSAPI:Find_Duplicate_Ext_Doc_No"
                       style="document"/>
            <input name="Find_Duplicate_Ext_Doc_No">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </input>
            <output name="Find_Duplicate_Ext_Doc_No_Result">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </output>
        </operation>
        <operation name="Get_Item_Quantity">
            <operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                       soapAction="urn:microsoft-dynamics-schemas/codeunit/myWSAPI:Get_Item_Quantity" style="document"/>
            <input name="Get_Item_Quantity">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </input>
            <output name="Get_Item_Quantity_Result">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </output>
        </operation>
        <operation name="GetSalesOrderETA">
            <operation xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                       soapAction="urn:microsoft-dynamics-schemas/codeunit/myWSAPI:GetSalesOrderETA" style="document"/>
            <input name="GetSalesOrderETA">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </input>
            <output name="GetSalesOrderETA_Result">
                <body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="myWSAPI">
        <port name="myWSAPI_Port" binding="tns:MyWSAPI_Binding">
            <address xmlns="http://schemas.xmlsoap.org/wsdl/soap/"
                     location="http://test.myapi.local:7947/my/path/here/myWSAPI"/>
        </port>
    </service>
</definitions>  

I'm trying to make a call to the “Get_Item_Quantity” function in the web service.

Function name: Get_Item_Quantity

This function takes Type, Code fields and returns Qty1, Qty2, Qty3, and Qty4 Parameters:

Type = 0 – input value
Code = ‘product100317’ – input value
Qty 1 – return value
Qty 2 – return value
Qty 3 – return value
Qty 4 – return value

I created a file CustomInterface.php

<?php

namespace Test\CustomApi\Api;

interface CustomInterface
{
    /**
     * GET for Qty api
     * @param int $type
     * @param string $code
     * @return string
     */

     public function getQty($type,$code);
}

and a file called Custom.php

<?php

namespace Test\CustomApi\Model\Api;
    
    use Psr\Log\LoggerInterface;
    
    class Custom
    {
        protected $logger;
    
        public function __construct(
            LoggerInterface $logger
        )
        {
            $this->logger = $logger;
        }
    
        /**
         * @inheritdoc
         */
        public function getQty($type,$code) {
            $response = ['success' => false];
    
            try {
                // Logic to call soap service here
                $qty = $this->getItemQty($type,$code);
                $response = ['success' => true, 'qty' => $qty];
    
            } catch (\Exception $e) {
                $response = ['success' => false, 'message' => $e->getMessage()];
                $this->logger->info($e->getMessage());
            }
    
            $returnArray = json_encode($response);
            return $returnArray;
        }
    
    
        /**
         * Soap API call 
         *
         * @param int $type
         * @param string $code
         * @return void
         */
        public function getItemQty($type,$code)
        {
            $soapRequest = '<?xml version="1.0" encoding="utf-8"?>
            <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
              <soap12:Body>
                <Get_Item_Quantity xmlns="http://test.com/">
                    <Type>' . $type . '</Type>
                    <Code>' . $code . '</Code>
                </Get_Item_Quantity>
              </soap12:Body>
            </soap12:Envelope>';
            $header = array(
                "Content-type: text/xml;charset=\"utf-8\"",
                "Accept: text/xml",
            );
            $soapHandler = curl_init();
            curl_setopt($soapHandler, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
            curl_setopt($soapHandler, CURLOPT_URL, "http://test.com/");
            curl_setopt($soapHandler, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($soapHandler, CURLOPT_CONNECTTIMEOUT, 0);
            curl_setopt($soapHandler, CURLOPT_POST, true);
            curl_setopt($soapHandler, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($soapHandler, CURLOPT_POSTFIELDS, $soapRequest);
            curl_setopt($soapHandler, CURLOPT_HTTPHEADER, $header);
            curl_setopt($soapHandler, CURLOPT_FOLLOWLOCATION, false);
            //header('Content-type: text/xml');
            $result = curl_exec($soapHandler);
            $qtyData = array();
            if ($result === false) {
                $err = 'Curl error: ' . curl_error($soapHandler);
                curl_close($soapHandler);
                $this->logger->info($err);
            } else {
                curl_close($soapHandler);
                $xml = simplexml_load_string($result);
                $xml->registerXPathNamespace('soap', 'http://www.w3.org/2003/05/soap-envelope');
                $result = $xml->xpath('/soap:Envelope/soap:Body');
                $res = $result[0]->Get_Item_Quantity_Result;
                $qtyData = simplexml_load_string($res);
            }
            return $qtyData;
        }
    }

Is there a way to add these files into postman and the endpoint link to test if i can populate the parameters? Is there an easier way to do it through command line and curl? I'm open for any avenue. Kind of a newbie to postman and API calls.

0 Answers
Related