Post response from Fedex stripped XML tags in Netsuite

Viewed 64

I am using UserEventScript to request fedex with tracking number for asking tracking detail.

        var headerObj = [];
        headerObj['Content-Type'] = 'text/xml';
        headerObj['Accept'] =  'text/xml';

        var body =
            "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" 
                 xmlns:v19=\"http://fedex.com/ws/track/v19\">\n" +
            "    <soapenv:Header />\n" +
            "    <soapenv:Body>\n" +
            "        <v19:TrackRequest>\n" +
            "            <v19:WebAuthenticationDetail>\n" +
            "                    <v19:ParentCredential>\n" +
            "                        <v19:Key>Your key</v19:Key>\n" +
            "                        <v19:Password>Your password</v19:Password>\n" +
            "                    </v19:ParentCredential>\n" +
            "                <v19:UserCredential>\n" +
            "                    <v19:Key>Your key</v19:Key>\n" +
            "                    <v19:Password>Your password</v19:Password>\n" +
            "                </v19:UserCredential>\n" +
            "            </v19:WebAuthenticationDetail>\n" +
            "            <v19:ClientDetail>\n" +
            "                <v19:AccountNumber>Your ac no.</v19:AccountNumber>\n" +
            "                <v19:MeterNumber>Your meter no.</v19:MeterNumber>\n" +
            "            </v19:ClientDetail>\n" +
            "            <v19:TransactionDetail>\n" +
            "                <v19:CustomerTransactionId>Track By 
                                  Number_v19</v19:CustomerTransactionId>\n" +
            "                <v19:Localization>\n" +
            "                    <v19:LanguageCode>EN</v19:LanguageCode>\n" +
            "                    <v19:LocaleCode>US</v19:LocaleCode>\n" +
            "                </v19:Localization>\n" +
            "            </v19:TransactionDetail>\n" +
            "            <v19:Version>\n" +
            "                <v19:ServiceId>trck</v19:ServiceId>\n" +
            "                <v19:Major>19</v19:Major>\n" +
            "                <v19:Intermediate>0</v19:Intermediate>\n" +
            "                <v19:Minor>0</v19:Minor>\n" +
            "            </v19:Version>\n" +
            "            <v19:SelectionDetails>\n" +
            "                <v19:CarrierCode>FDXE</v19:CarrierCode>\n" +
            "                <v19:PackageIdentifier>\n" +
            "                    <v19:Type>TRACKING_NUMBER_OR_DOORTAG</v19:Type>\n" +
            "                    <v19:Value>992959690382</v19:Value>\n" +
            "                </v19:PackageIdentifier>\n" +
            "                <v19:ShipmentAccountNumber />\n" +
            "                <v19:SecureSpodAccount />\n" +
            "            </v19:SelectionDetails>\n" +
            "            <v19:ProcessingOptions>INCLUDE_DETAILED_SCANS</v19:ProcessingOptions>\n" +
            "        </v19:TrackRequest>\n" +
            "    </soapenv:Body>\n" +
            "</soapenv:Envelope>"

        var resoponse = https.post({
            url: 'https://ws.fedex.com:443/web-services',
            body: body,
            headers:headerObj
        });

And the response.body i got is like this.

SUCCESSSUCCESStrck0Request was successfully processed.Request was successfully processed.Track By Number_v19ENUStrck1900SUCCESSSUCCESStrck0Request was successfully processed.Request was successfully processed.falsefalse0SUCCESStrck0Request was successfully processed.Request was successfully processed.9929596903822459294000~992959690382~FX2021-03-21T00:00:00ARArrived at FedEx locationMEMPHISTNUSUnited StatesfalseNo scheduled delivery date available at this time.EDDUNAVAILABLEFDXEFedEx ExpressSHIPPER_REFERENCEB2003217, STAR DIAM LTDINTERNATIONAL_PRIORITYInternational PriorityIPLB1.013123INLB1.0FEDEX_BOXFedEx Box1100AWBDELIVER_WEEKDAYDeliver WeekdayOTHERDUTIES_AND_TAXESRECIPIENT_ACCOUNTRecipientTRANSPORTATIONSHIPPER_ACCOUNTShipperHUNGHOM,HKHong Kong SAR, ChinafalseACTUAL_PICKUP2021-03-20T14:39:00+08:00SHIP2021-03-20T00:00:00ACTUAL_TENDER2021-03-20T14:39:00+08:00ATLANTA,GAUSUnited Statesfalse00ON_DELIVERYON_EXCEPTIONON_ESTIMATED_DELIVERYINDIRECT_SIGNATURE_RELEASEINELIGIBLEREDIRECT_TO_HOLD_AT_LOCATIONPOSSIBLY_ELIGIBLEREROUTEINELIGIBLERESCHEDULEINELIGIBLE2021-03-21T08:19:00-05:00ARArrived at FedEx location MEMPHISTN38118USUnited Statesfalse FEDEX_FACILITY2021-03-20T23:17:00-08:00DPDeparted FedEx location ANCHORAGEAK99502USUnited Statesfalse FEDEX_FACILITY2021-03-20T21:58:00-08:00ARArrived at FedEx location ANCHORAGEAK99502USUnited Statesfalse FEDEX_FACILITY2021-03-21T04:35:00+08:00ITIn transit CHEK LAP KOK300HKHong Kong SAR, Chinafalse FEDEX_FACILITY2021-03-20T18:09:00+08:00DPLeft FedEx origin facility TSUEN WAN230HKHong Kong SAR, Chinafalse ORIGIN_FEDEX_FACILITY2021-03-20T14:39:00+08:00PUPicked up TSUEN WAN230HKHong Kong SAR, Chinafalse PICKUP_LOCATION

Actually, it matches with the normal result i got from postman, but it is unreadable. What i should do to transfer the above one to be a normal json format or xml format? Or how can i get the exactly value from the above response?

1 Answers

You are not showing how you get that body but I suspect you are just logging it. What the FedEx API returns is xml. If you log that Netsuite doesn't escape the tags for you so it will look just like that in the log. If you view the log and inspect the details you should see all the tags.

to get NS to escape that for you try:

log.debug({
    title:'FedEx Response',
    details:xml.escape({xmlText:resp.body})
});

Then you can use code like that below to extract the info you are looking for:

var xmlDocument = xml.Parser.fromString({
            text: resp.body
        });
var ref = xml.XPath.select({
            node: xmlDocument,
            xpath: '//ns:TrackingNumber' /* or maybe //nlapi:TrackingNumber or //TrackingNumber */
        });

Once you have the full xml by escaping the response you can see the namespaces used in the response. I think the first form is what you'll use but you may need the nlapi: form if the prefix xmlns prefix for ns is also the default namespace.

Related