> For the complete documentation index, see [llms.txt](https://checksound.gitbook.io/tecnologie5/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://checksound.gitbook.io/tecnologie5/web-services/soap.md).

# SOAP - Simple Object Access Protocol

### ESEMPIO CHIAMATA a servizio SOAP

Free, public SOAP web services example:&#x20;

{% embed url="<http://www.learnwebservices.com/>" %}

Vedere esempio utilizzando [SoapUI ](https://www.soapui.org/downloads/soapui.html) tool per fare chiamate a servizi soap partendo dal **Web Services Description Language** (WSDL) esposto dal web service.

Tramite *SoapUI*, inserendo l'url del **WSDL** esposta dal web service, vi genera dei template XML della request SOAP e permette di invocare i metodi del web service.

Ad esempio utilizzando il WSDL: <https://apps.learnwebservices.com/services/tempconverter?wsdl>

SoapUI parsando il WSDL crea questo template per la richiesta *CelsiusToFahrenheit*:

```markup
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://learnwebservices.com/services/tempconverter">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:CelsiusToFahrenheitRequest>
         <tem:TemperatureInCelsius>?</tem:TemperatureInCelsius>
      </tem:CelsiusToFahrenheitRequest>
   </soapenv:Body>
</soapenv:Envelope>
```

Inserendo ad esempio il valore *18* ad esempio, come contenuto dell'elemento `tem:TemperatureInCelsius`, facendo la request `POST HTTP` a [https://apps.learnwebservices.com/services/tempconverter](https://apps.learnwebservices.com/services/tempconverter?wsdl) si ottiene la seguente risposta dal web service:

```markup
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <CelsiusToFahrenheitResponse xmlns="http://learnwebservices.com/services/tempconverter">
         <TemperatureInFahrenheit>64.4</TemperatureInFahrenheit>
      </CelsiusToFahrenheitResponse>
   </soap:Body>
</soap:Envelope>
```

con il valore convertito in gradi Fahrenheit.

Se ad esempio nella request utilizzo un valore come *AA* contenuto dell'elemento `tem:TemperatureInCelsius`, si ottiene il messaggio di errore SOAP:

```markup
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Client</faultcode>
         <faultstring>Unmarshalling Error: AA  If you use comma (,) character for decimal separator, change it to dot (.) character!</faultstring>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>
```

perché nella richiesta utilizziamo un valore non ammesso in base al formato della richiesta specificato nel WSDL.

*SoapUI* è utilizzato per eseguire test a un applicazione, noi lo utilizziamo come comodo client per invocare i nostri web service.

### Esempio con client JavaScript

Esempio di un invocazione di un web-service SOAP con JavaScript:

```javascript
const url = "https://apps.learnwebservices.com/services/hello";
const request = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <HelloRequest xmlns="http://learnwebservices.com/services/hello">
            <Name>John Doe</Name>
        </HelloRequest>
    </soapenv:Body>
 </soapenv:Envelope>`;

 const fetchData = {
    method: 'POST',
    body: request
 };

 fetch(url, fetchData)
   .then(function(response) {
     return response.text();
   })
   .then(function(xml) {
       const xmlDoc = new DOMParser().parseFromString(xml, "text/xml");
       console.log(xmlDoc.getElementsByTagNameNS("http://learnwebservices.com/services/hello", "Message")[0].textContent);
   })
   .catch(function(error) {
     console.log("Error calling webservice: " + error);
   });
```

### Esercizi

Utilizza il client [SoapUI](https://www.soapui.org/) o [Postman](https://www.postman.com/) (esiste di quest'ultimo anche la possibilità di utilizzare il servizio web senza scaricare il client) per importare il WSDL del convertitore di temperature (esempio di sopra) e fare alcune chiamate ai metodi esposti dal web service.
