5

I'm looking for a way to send SOAP request to a web service, with a WSDL. Is it possible to do that with Typescript 2 and Angular 2 ?

I've seen tutorials for Angular 1 but they used old angular methodes, like factory or controller.

I would like if it's possible, a new way to do that with TypeScript.

Any ideas ?

Couim
  • 735
  • 3
  • 12
  • 29

2 Answers2

4

What you need is a service that wraps around Http and provides deserialization:

@Injectable()
export class SOAPService{

    constructor(private http:Http){}

    public get(url:string, options?:RequestOptionsArgs):Observable<any>{
        return this.http.get(url, options).map(res => {
            let xmlresult = res.text();
            let result = //Here you deserialize your xml object
            return result;
        }
    }
}

Then you can use it this way:

@Component({...})
export class ExampleComponent{
    constructor(private soap:SOAPService){}


    foo():{
        this.soap.get('foo/bar').subscribe(...);
    }
}

Since I'm not an xml parsing expert, I can't tell you how to deserialize your XML, but you can check MDN for that, you simply have to wrap the serialization/deserialization process in another service and inject it in your SOAPService.

Supamiu
  • 8,501
  • 7
  • 42
  • 76
  • Correct me if I am wrong, but this is the same as making your own wrapper around Angular2's `Http` class, right? You could might as well just use the `Http` class as it is? – John Oct 06 '16 at 08:55
  • This is a wrapper around Angular2's `Http` class ;). Of course you might use `Http` directly, but problem is that it would involve a lot of repeated code (everywhere you need SOAP, you'll repeat the deserialization logic). – Supamiu Oct 06 '16 at 08:57
  • Okay so I am trying your response, but I don't understand how to give `RequestOptionsArgs` parameter from my app component? How can I declare it? And what does the `subscribe` function? Thanks in advance :) – Couim Oct 06 '16 at 11:22
  • `options` param is optional, like it is for `Http`, you don't have to provide it unless you want some specific options in your request (specific headers, content-types, etc). The subscribe function allows you to connect on the async behaviour of `Http`, it's like if you passed a callback to process the final data received. – Supamiu Oct 06 '16 at 11:28
  • @Supamiu Okay thanks, and if I want to call a specific function which is running on my web service, where can I secify it ? :) – Couim Oct 06 '16 at 12:27
  • It's the url, you should probably have an endpoint on your SOAP Webservice that's binded to a RPC call, this is the url you want to pass as url parameter. – Supamiu Oct 06 '16 at 12:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125112/discussion-between-calliste-hanriat-and-supamiu). – Couim Oct 06 '16 at 13:16
1

You could use a regular http-request with Angular2's [Http class] (https://angular.io/docs/ts/latest/api/http/index/Http-class.html) This is an example from their page:

import {Http, HTTP_PROVIDERS} from '@angular/http';
import 'rxjs/add/operator/map'
@Component({
  selector: 'http-app',
  viewProviders: [HTTP_PROVIDERS],
  templateUrl: 'people.html'
})
class PeopleComponent {
  constructor(http: Http) {
    http.get('people.json')
      // Call map on the response observable to get the parsed people object
      .map(res => res.json())
      // Subscribe to the observable to get the parsed people object and attach it to the
      // component
      .subscribe(people => this.people = people);
  }
}

instead of asking for a json file, you could use a URL instead.

John
  • 10,165
  • 5
  • 55
  • 71