Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post data to SOAP api with angular js

I am trying to post data to soap api but unable to do so. i have tried all possible methods but still i get error while calling api.

my api is - http://xyz.asmx?op=UserRegistration

and it excepts data in xml format like

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <UserRegistration xmlns="http://Service/">
      <Usercreditional>string</Usercreditional>
    </UserRegistration>
  </soap:Body>
</soap:Envelope>

Things i have tried -

1> With $http.post

    var soapData = '<?xml version="1.0" encoding="utf-8"?>'+
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
    '<soap:Body>'+
    '<UserRegistration xmlns="http://Service/">'+
    '<Usercreditional>[{ \'DeviceUUID\': \'' + data.DeviceUUID + '\', ' +         
                      "\"DevicePushID\":\"" + data.DevicePushID + "\"}]" +
                    '</Usercreditional></UserRegistration></soap:Body></soap:Envelope>';

return $http({ 
            method: 'POST', 
            url: ' http://xyz.asmx?op=UserRegistration', 
            data : soapData, 
            headers : { "Content-Type" : 'application/xml'}
        });

this gives error "unable to process request. ---> Root element is missing"

2> With SOAPClient

 var deferred = $q.defer(); 
 var soapParams = new SOAPClientParameters(); 
 var userCredentials = [{"DeviceUUID": data.DeviceUUID, "DevicePushID": data.DevicePushID}]; 
 for (var param in userCredentials )
   { 
      soapParams.add(param, soapParams[param]);
   } 
 var soapCallback = function (e) {
    if (e.constructor.toString().indexOf("function Error()") != -1) {
         deferred.reject(e);
       } else {
         deferred.resolve(e);
            }
      };        SOAPClient.invoke(' http://xyz.asmx', 'UserRegistration', soapParams, true, soapCallback);
        return deferred.promise;

this is giving error Cannot read property 'getElementsByTagName' of null

can anyone please help me in this one ? tried almost everything still no luck. thanks in advance

like image 571
Vikas Vanvi Avatar asked Apr 26 '17 09:04

Vikas Vanvi


2 Answers

  1. With $http.post

You can use the $http service to POST data to SOAP APIs as below:

enter image description here

var headers = {
                'Content-Type': 'text/xml; charset=utf-8'
            };
 return $http({ 
                method: 'POST', 
                url: 'http://providerservice.fmcnetwork.net/MobileAppService.asmx?op=UserRegistration', 
                data : soapData, 
                headers : headers
            });
  1. With angular-soap

You can also use the plugin angular-soap built on the top of SOAP Client and use $soap service to achieve the same.

For example:

angular.module('myApp', ['angularSoap'])

.factory("testService", ['$soap',function($soap){
    var base_url = "http://www.cooldomain.com/SoapTest/webservicedemo.asmx";

    return {
        CreateUser: function(firstName, lastName){
            return $soap.post(base_url,"CreateUser", {firstName: firstName, lastName: lastName});
        }
    }
}])

.controller('MainCtrl', function($scope, testService) {

  testService.CreateUser($scope.firstName, $scope.lastName).then(function(response){
    $scope.response = response;
  });

})
like image 72
Tessy Thomas Avatar answered Oct 23 '22 01:10

Tessy Thomas


Try this :)

  var soapData = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">'+
    '<Body>'+
    '<UserRegistration xmlns="http://FmcMobileApplicationService/">'+
    '<Usercreditional>{ \'DeviceUUID\': \'' + data.DeviceUUID + '\', ' +         
                      "\"DevicePushID\":\"" + data.DevicePushID + "\"}" +
                    '</Usercreditional></UserRegistration></Body></Envelope>';

return $http({ 
            method: 'POST', 
            url: 'http://providerservice.fmcnetwork.net/MobileAppService.asmx'
            data : soapData, 
            headers : { "Content-Type" : 'application/xml'}
        });

Changes from your code

  • Didn't send the xml header
  • Changed soap:Envelope to Envelope
  • Took out the schema namespaces
  • Renamed soap:Body to body
  • Posted to MobileAppService.asmx without the querystring
  • Sent an object { "DeviceUUID": "1", "DevicePushID": "2" } not an array

I got a response. It was an empty once but a real one :) I used 1 and 2 as DeviceUUID and DevicePushID respectively. Hence an empty response I assume.

Response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <UserRegistrationResponse xmlns="http://FmcMobileApplicationService/">
            <UserRegistrationResult>[]</UserRegistrationResult>
        </UserRegistrationResponse>
    </soap:Body>
</soap:Envelope>
like image 45
Bergur Avatar answered Oct 23 '22 00:10

Bergur