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

2.2k views Asked by At

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 it's throwing errors 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.

1

There are 1 answers

0
CodeChanger On BEST ANSWER

After struggling a lot on this issue I found a solution to clean my request cookies.

In the HTTP Advanced plugin, there is one method to clear my cookies.

clearCookies()

Clear all cookies.

Use this method before calling any API.

So what it will do clear all my cookies and my issue related to old cookies will be solved in this way.

constructor(
    public storage: Storage,
    public httpPlugin: HTTP,
    private platform: Platform
  ) {
    // enable SSL pinning
    httpPlugin.setSSLCertMode("pinned");
    //Clear old cookies
    httpPlugin.clearCookies();
  }

The above code solves my issue.

Thanks all for your quick guidance and suggestions.

comment on this if this is not the right way to clear my old request data.