DEV Community

KISHAN RAMLAKHAN NISHAD
KISHAN RAMLAKHAN NISHAD

Posted on

How to make Middleware of soap api

BELOW IS CRUL OF SAP API NEED TO MAKE MIDDLEWARE API

curl --location --request POST 'https://mwuat.icicibankltd.com/v1/api/peoplesoft/viewMaritalStatus' \
--header 'Content-Type: text/xml;charset=UTF-8' \
--header 'SOAPAction: MarStatusfetch.v1 ' \
--header 'Cookie: hrmsuat=ffffffff094614af45525d5f4f58455e445a4a421640' \
--data-raw '<XML>
<MessageType>1200</MessageType>
<ProcCode>PS_VIEWMS</ProcCode>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ici="http://xmlns.oracle.com/Common/schemas/ICI_MAR_STS_ERBOT_REQ.V1">
<soapenv:Header/>
<soapenv:Body>
<ici:ICI_MAR_STS_ERBOT_REQ>
<ici:EMPLID>90002346</ici:EMPLID>
</ici:ICI_MAR_STS_ERBOT_REQ>
</soapenv:Body>
</soapenv:Envelope>
</XML>'

ROUTES

  app.post('/viewMaritalStatus',controller.uotmauth.viewMaritalStatus)

Enter fullscreen mode Exit fullscreen mode

CONTROLLER

exports.viewMaritalStatus = async (req, res, next) => {
  try {
    const emplid = req.body.EMPLID;
    console.log(emplid);
    const result = await get_viewMaritalStatus_ResponseXML(emplid);
    console.log('results', result);
    const response = await encryptResponse({ message: 'success', data: result, status: 200 });
    TransactionlogError(req.body.EMPLID, req.originalUrl, '', '', 'success');
    res.send(response);
  } catch (error) {
    TransactionlogError(req.body.EMPLID, req.originalUrl, req.body, error, 'fail');
    handleError(req, res, error);
  }
};



const get_viewMaritalStatus_ResponseXML = async emplid => {
  const url =
    'https://mwuat.icicibankltd.com/v1/api/peoplesoft/viewMaritalStatus';

  // SOAP XML Payload
  const xmlPayload = `<XML>
<MessageType>1200</MessageType>
<ProcCode>PS_VIEWMS</ProcCode>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ici="http://xmlns.oracle.com/Common/schemas/ICI_MAR_STS_ERBOT_REQ.V1">
<soapenv:Header/>
<soapenv:Body>
  <ici:ICI_MAR_STS_ERBOT_REQ>
    <ici:EMPLID>${emplid}</ici:EMPLID>
  </ici:ICI_MAR_STS_ERBOT_REQ>
</soapenv:Body>
</soapenv:Envelope>
</XML>`;

  const headers = {
    SOAPAction: 'MarStatusfetch.v1',
    'Content-Type': 'text/xml;charset=UTF-8',
  };

  try {
    // Sending the SOAP Request
    const response = await axiosapi.post(url, xmlPayload, { headers });

    console.log(response);
    console.log(response.data);
    return new Promise((resolve, reject) => {
      parseString(response.data, { explicitArray: false }, (err, result) => {
        if (err) {
          console.error('Error in parsing XML response:', err);
          reject(err);
        } else {
          // Navigate through the XML structure to get to the SOAP Body
          const data = result['XML']['soapenv:Envelope']['soapenv:Body'];
          console.log('datadata:', data);
          resolve(data);
        }
      });
    });
  } catch (error) {
    console.error('Error in SOAP request:', error.message);
    throw new Error('Failed to fetch marital status data');
  }
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)