> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Preparing and Sending SOAP Message with XmlSlurper

> Preparing SOAP message with Groovy script and sending with HTTP request

## Groovy Script

```groovy theme={null}
import groovy.util.XmlSlurper

URL url = new URL("https://servis.apinizer.com/SoapExampleService.svc")
HttpURLConnection conn = url.openConnection()
conn.setDoOutput(true)
var password='''<![CDATA[ ]]>'''
conn.setRequestProperty("SOAPAction", "http://tempuri.org/SoapExampleService/GetToken")
conn.setRequestProperty("Content-Type", "text/xml")
var xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"> <soapenv:Header/> <soapenv:Body> <tem:GetToken> <tem:userName>kullaniciAdi</tem:userName> <tem:password>"+password+"</tem:password> </tem:GetToken> </soapenv:Body></soapenv:Envelope>";
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream())
wr.write(xml)
wr.close()
InputStream responseStream = conn.getInputStream()
String response = responseStream.getText("utf-8")
responseStream.close()
def Envelope = new XmlSlurper().parseText(response)
requestHeaderMapToTargetAPI.put("TokenId", Envelope.Body.GetTokenResponse.GetTokenResult.text() )
```

## Explanation

This script performs the following operations:

1. **SOAP Message Preparation**: SOAP Envelope, Header and Body are created
2. **HTTP Request**: SOAP message is sent with HTTP POST request
3. **Response Parse**: Incoming SOAP response is parsed
4. **Token Extraction**: Token value is extracted from response
5. **Adding to Header**: Token value is added to header

<Note>
  This script should be run on the request line (Request Policy) because it uses the `requestHeaderMapToTargetAPI` variable.
</Note>
