Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send XML data using Axios Library

I'm new for coding, currently i'm exploring on Axios to send XML request, appreciate for your help on how to translate below into Axios command?

Request body

<?xml version="1.0" encoding="UTF-8"?>
<req:KnownTrackingRequest xmlns:req="http://www.example.com" 
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                    xsi:schemaLocation="http://www.example.com
                    TrackingRequestKnown.xsd">
<Request>
    <ServiceHeader>
        <MessageTime>2002-06-25T11:28:56-08:00</MessageTime>
        <MessageReference>1234567890123456789012345678</MessageReference>
        <SiteID>SiteID</SiteID>
        <Password>Password</Password>
    </ServiceHeader>
</Request>
<LanguageCode>en</LanguageCode>
<AWBNumber>01234567890</AWBNumber>
<LevelOfDetails>LAST_CHECK_POINT_ONLY</LevelOfDetails>

like image 559
Adam Avatar asked Oct 30 '18 06:10

Adam


People also ask

Does Axios support XML?

You can use XML instead of JSON in axios as follows.

How do I post data on Axios?

To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.

How do I use Axios to fetch data in node JS?

Create an index. js file and write the following code in it. const express = require("express"); const app = express(); const axios = require("axios"). create({baseUrl: "https://jsonplaceholder.typicode.com/"}); app.


1 Answers

You can use XML instead of JSON in axios as follows.

var xmlBodyStr = `<?xml version="1.0" encoding="UTF-8"?>
       <req:KnownTrackingRequest xmlns:req="http://www.example.com" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xsi:schemaLocation="http://www.example.com
                TrackingRequestKnown.xsd">
         <Request>
           <ServiceHeader>
              <MessageTime>2002-06-25T11:28:56-08:00</MessageTime>
              <MessageReference>1234567890123456789012345678</MessageReference>
              <SiteID>SiteID</SiteID>
              <Password>Password</Password>
           </ServiceHeader>
         </Request>
         <LanguageCode>en</LanguageCode>
         <AWBNumber>01234567890</AWBNumber>
         <LevelOfDetails>LAST_CHECK_POINT_ONLY</LevelOfDetails>`;

var config = {
     headers: {'Content-Type': 'text/xml'}
};

axios.post('https://POST_URL', xmlBodyStr, config); 

below is my full code:

    const axios = require('axios');
const parseString = require('xml2js').parseString;
exports.handler = function(context, event, callback) {
var xmlBodyStr = `<?xml version="1.0" encoding="UTF-8"?>
   <req:KnownTrackingRequest xmlns:req="http://www.sample.com" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://www.sample.com
            TrackingRequestKnown.xsd">
     <Request>
       <ServiceHeader>
          <MessageTime>2002-06-25T11:28:56-08:00</MessageTime>
          <MessageReference>1234567890123456789012345678</MessageReference>
          <SiteID>ID</SiteID>
          <Password>Pwd</Password>
       </ServiceHeader>
     </Request>
     <LanguageCode>en</LanguageCode>
     <AWBNumber>0123456789</AWBNumber>
     <LevelOfDetails>LAST_CHECK_POINT_ONLY</LevelOfDetails>`;
var config = {
 headers: {'Content-Type': 'text/xml'}
};
 axios.post('https://xml.sample.com/XMLShippingServlet', xmlBodyStr, config).then(res => {
callback(res.data);
}).catch(err => callback(err));
};    

XML Response:

<?xml version="1.0" encoding="UTF-8"?>
<req:TrackingResponse xmlns:req="http://www.dhl.com" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com TrackingResponse.xsd">
<Response>
    <ServiceHeader>
        <MessageTime>2018-11-02T04:29:21.024+01:00</MessageTime>
        <MessageReference>1234567890123456789012345678</MessageReference>
        <SiteID>ID</SiteID>
    </ServiceHeader>
</Response>
<AWBInfo>
    <AWBNumber>1234567890</AWBNumber>
    <Status>
        <ActionStatus>success</ActionStatus>
    </Status>
    <ShipmentInfo>
        <OriginServiceArea>
            <ServiceAreaCode>PEN</ServiceAreaCode>
            <Description>PENANG-MYS</Description>
        </OriginServiceArea>
        <DestinationServiceArea>
            <ServiceAreaCode>PAO</ServiceAreaCode>
            <Description>Description</Description>
        </DestinationServiceArea>
        <ShipperName>Shipper</ShipperName>
        <ShipperAccountNumber>12354678</ShipperAccountNumber>
        <ConsigneeName>Sample</ConsigneeName>
        <ShipmentDate>2018-09-21T02:41:21</ShipmentDate>
        <Pieces>1</Pieces>
        <Weight>0.5</Weight>
        <WeightUnit>K</WeightUnit>
        <GlobalProductCode>P</GlobalProductCode>
        <ShipmentDesc>testing</ShipmentDesc>
        <DlvyNotificationFlag>N</DlvyNotificationFlag>
        <Shipper>
            <City>CHEMOR</City>
            <PostalCode>12345</PostalCode>
            <CountryCode>MY</CountryCode>
        </Shipper>
        <Consignee>
            <City>SUNNYVALE</City>
            <CountryCode>US</CountryCode>
        </Consignee>
        <ShipmentEvent>
            <Date>2018-09-21</Date>
            <Time>11:30:52</Time>
            <ServiceEvent>
                <EventCode>OK</EventCode>
                <Description>Delivered</Description>
            </ServiceEvent>
            <Signatory>Cnee</Signatory>
            <ServiceArea>
                <ServiceAreaCode>ABC</ServiceAreaCode>
                <Description>sample</Description>
            </ServiceArea>
        </ShipmentEvent>
    </ShipmentInfo>
</AWBInfo>
<LanguageCode>en</LanguageCode>
</req:TrackingResponse>
<!-- ServiceInvocationId:20181102042921_7837_c7ab8c82-428a-4b59-8379- 
a00ec8a4f29e -->
like image 129
Bharathvaj Ganesan Avatar answered Sep 17 '22 19:09

Bharathvaj Ganesan