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

1

9

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.

Bari answered 8/10, 2018 at 5:11 Comment(11)
I have a similiar problem. angular/http or native-http, both fail on passing data to the server. The same code works for android but not for ios.Nobility
Did you manage to solve it?Nobility
I couldn't send the data through POST, because the library wasn't doing the proper serialization. My api also accepts GET, so Im now reaching the API through GET and passing the parameters in the url, using encodeURI(urlWithParameters, null, null)Nobility
As my API not support GET only post allowed in my case so still not solve above issue struggling since long time but didnt find any solution.Bari
Have you compared the request headers that are sent by Postman with the ones sent by your application? Sometimes it's just one minor mistake or one missing header causing the problem. Make the same comparison for the request body. If both are the same, one would assume that the server would respond with the same result.Elison
Yes I have checked that both are same expect Postman Uniq id other than all header and body I am passing same as postman.Bari
Did you remote debug the problem on the device already? Follow these instructions here to debug the problem in Safari dev tools: ionic.zone/debug/remote-debug-your-app#ios Follow these instructions here to debug the problem in Chrome dev tools: ionic.zone/debug/remote-debug-your-app#android Look at the console and network tabs for errorsGranvillegranvillebarker
Is your server listening on https ? in this case make sure there is a valid TLS certificate on it, otherwise IOS will reject the request due to security issue. Please also provide the error log, it will be helpfulFantoccini
Yes my server listening on HTTPS and other services are working find just this XML based service not working.Bari
@tiagoperes I already debug on device and on both Android and iOS debugger I m getting above error.Bari
where is above?Granvillegranvillebarker
B
0

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.

Bari answered 8/3, 2019 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.