Wednesday, 24 October 2018

XML API calling not working with cordova-plugin-advanced-http latest version in ionic3

I am using cordova-plugin-advanced-http plugin for API calling and all JSON enabled API working fine but I have one XML embedded API which is working fine in Postman but while I call it from ionic its param not getting at the server end.

Below is my code for XML API:

Type 1:

let headers = {
          "Content-type": 'text/xml; charset=utf-8',
          "Authorization": token,
        };

    let xmlBody =
      '<ServiceRequest>' +
      '<CaseNumber>' + caseNumber +
      '</CaseNumber>' +
      '</ServiceRequest>'

    this.httpPlugin.setDataSerializer('utf8');

    this.httpPlugin.post('https://test.com/Service', xmlBody, headers).then((response) => {
      console.log("XML Response : ", JSON.stringify(response.data));
      xml2js.parseString(response.data, function (err, result) {
        console.log("XML parser success:", result);
        console.log("XML parser error:", err);
        if (result) {
          resolve(result);
        } else {
          reject(err);
        }

      });
    }).catch(error => {
      if (error.status == 403) {
        console.log("Token expired : " + JSON.stringify(error));
      } else {
        console.log("Error : " + error.error);
        console.log("Error " + JSON.stringify(error));
        reject(error);
      }
    });

Type 2:

    let xmlBody = '<ServiceRequest>' +
      '<CaseNumber>' + caseNumber +
      '</CaseNumber>' +
      '</ServiceRequest>';

    console.log("XML Body", xmlBody)

    // this.httpPlugin.setRequestTimeout(60);
    this.httpPlugin.setDataSerializer('utf8');

    this.httpPlugin.setHeader('*', 'authorization', token);
    this.httpPlugin.setHeader('*', 'Content-Type', "application/x-www-form-urlencoded");

    this.httpPlugin.post('https://test.com/Service', xmlBody, {}).then((response) => {
      console.log("XML Response : ", JSON.stringify(response.data));
      xml2js.parseString(response.data, function (err, result) {
        console.log("XML parser success:", result);
        console.log("XML parser error:", err);
        if (result) {
          resolve(result);
        } else {
          reject(err);
        }

      });

    }).catch(error => {
      if (error.status == 403) {
        console.log("Token expired : " + JSON.stringify(error));
      } else {
        console.log("Error : " + error.error);
        console.log("Error " + JSON.stringify(error));
        reject(error);
      }

    });

All the time its throwing error from the server and with the same request I am able to get data in postman as well as Native iOS code.

I referred this issue on GitHub but still no success.

Something I am missing though it's not able to get data on the server.

Help me to solve this issue.



from XML API calling not working with cordova-plugin-advanced-http latest version in ionic3

No comments:

Post a Comment