SoapReader
This library allows to consume SOAP
in XML format
Installation
npm install soap-reader
Steps
-
Create Tag
: If it is needed to send parameter(s), those should be send wrapped in its TAG nameFor Example : <'intA'>4<'/intA'>
How to do it?
import { SoapInputTag } from 'soap-reader';
let tagData: Array = [];
tagData.push(new SoapInputTag('intA', '4'));
Result: An List of array
[ {title:'intA',value:4}]
-
Create Headers
: Set up headers in order to get the response as a TEXT.IMPORTANT! -> This in an optional step so feel free to set up your header accordings your needs.
How to do it?
const httpOptions = this.soapReader.createHeaders();
Headers:
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/soap+xml; charset=utf-8', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET,POST,PATCH,DELETE,PUT,OPTIONS', 'Access-Control-Allow-Headers': 'Origin, Content-Type, X-Auth-Token, content-type', }), responseType: 'text' as 'json' };
-
Create Template
: create SOAP's TEMPLATE.How to do it?
@function
createTemplate
@description function to create SOAP's TEMPLATE.
@param methodName {string} SOAP method's name.
@param inputTagData {Array[SoapInputTag]} one or more SOAP's tags and its values.
@returns {string} SOAP's TEMPLATE
let soapMethodName = "Add"; let inputTagsData: Array<SoapInputTag> = []; inputTagsData.push(new SoapInputTag('intA', 4)); inputTagsData.push(new SoapInputTag('intB', 3)); let soapString = this.soapReader.createTemplate(this.soapMethodName, inputTagsData);
Result
<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <Add xmlns="http://tempuri.org/"> <intA>4</intA> <intB>3</intB> </Add> </soap12:Body> </soap12:Envelope>
Usage
export class CalculatorService {
private apiURL = 'www.dneonline.com/calculator.asmx?op=';
private soapMethodNameAdd = "Add";
private soapMethodNameAddResult = "AddResult";
constructor(private soapReader: SoapReaderService,
private http: HttpClient) { }
add(numberA: number, numberB: number): Observable<any> {
let apiURL = 'https://cors-anywhere.herokuapp.com/' + this.apiURL + this.soapMethodNameAdd;
let inputTagsData: Array<SoapInputTag> = [];
inputTagsData.push(new SoapInputTag('intA', numberA));
inputTagsData.push(new SoapInputTag('intB', numberB));
const httpOptions = this.soapReader.createHeaders();
let soapString = this.soapReader.createTemplate(this.soapMethodNameAdd, inputTagsData);
return this.http.post(apiURL, soapString, httpOptions).pipe(map(response => {
// get data in Array from a specific tag
return this.soapReader.getSingleStringResult(response.toString(), this.soapMethodNameAddResult);
}));
}
}
KeyWords
- SOAP
- XML
- ANGULAR
Authors
Please contact me at jclavotafur@gmail.com.
License
This project is licensed under the MIT License.