SOAP Web Services
Consume SOAP web services with BoxLang's fluent SOAP client
New in BoxLang 1.8.0 - BoxLang provides a powerful, fluent SOAP client for consuming SOAP 1.1 and SOAP 1.2 web services. The soap() BIF automatically parses WSDL documents, discovers available operations, and provides intelligent type conversion between SOAP XML and BoxLang types.
🚀 Quick Start
// Create SOAP client from WSDL
ws = soap( "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL" );
// Invoke operations directly as methods
countries = ws.ListOfContinentsByName();
dump( countries );
// Pass arguments
countryInfo = ws.CountryISOCode( { sCountryName: "United States" } );
dump( countryInfo );📋 Key Features
🔍 Automatic WSDL Parsing - Discovers operations, parameters, and types from WSDL
🎯 Fluent API - Call SOAP operations as native BoxLang methods
🔄 Automatic Type Conversion - Converts SOAP XML types to BoxLang types automatically
📦 Smart Response Unwrapping - Automatically unwraps single-property SOAP responses
🔒 Authentication Support - HTTP Basic Auth for secured services
⏱️ Configurable Timeouts - Control request timeouts
📊 SOAP 1.1 & 1.2 - Supports both SOAP versions with automatic detection
🔧 Custom Headers - Add custom HTTP headers as needed
📈 Statistics Tracking - Monitor invocations, successes, and failures
📋 Table of Contents
🎯 The soap() BIF
soap() BIFThe soap() BIF creates a fluent SOAP client from a WSDL URL. The client automatically discovers available operations and allows you to invoke them as native BoxLang methods.
Basic Syntax
Parameters
wsdlUrl
string
Yes
The URL to the WSDL document describing the web service
Return Value
Returns a BoxSoapClient instance configured with all operations discovered from the WSDL.
🔧 Configuration Methods
The SOAP client provides fluent configuration methods that can be chained:
timeout( seconds )
timeout( seconds )Set the request timeout in seconds.
withBasicAuth( username, password )
withBasicAuth( username, password )Configure HTTP Basic Authentication for secured services.
header( name, value )
header( name, value )Add a custom HTTP header to all requests.
soapVersion( version )
soapVersion( version )Override the SOAP version (normally auto-detected from WSDL).
Valid values: "1.1" or "1.2"
Chaining Configuration
All configuration methods return the client instance for fluent chaining:
🎬 Invoking SOAP Operations
Once you have a SOAP client, invoke operations directly as methods:
Direct Method Invocation
Using the invoke() Method
invoke() MethodYou can also use the invoke() method explicitly:
🌍 Real-World Examples
Example 1: Country Information Service
Example 2: Weather Service
Example 3: Payment Gateway
Example 4: CRM Integration
Example 5: Shipping Service
📊 Client Information Methods
The SOAP client provides several methods to inspect and understand the web service:
getOperationNames()
getOperationNames()Returns an array of all available operation names.
hasOperation( operationName )
hasOperation( operationName )Check if a specific operation exists.
getOperationInfo( operationName )
getOperationInfo( operationName )Get detailed information about a specific operation.
getStatistics()
getStatistics()Get client usage statistics.
toStruct()
toStruct()Convert the entire client to a struct representation.
🔄 Automatic Type Conversion
One of the most powerful features of BoxLang's SOAP client is automatic type conversion between SOAP XML types and BoxLang types.
XML Schema Types to BoxLang
BoxLang automatically converts XML Schema types to appropriate BoxLang types:
xsd:string
String
"hello"
xsd:int, xsd:integer
Integer
42
xsd:long
Long
9223372036854775807
xsd:float, xsd:double, xsd:decimal
Double
3.14159
xsd:boolean
Boolean
true, false
xsd:date, xsd:dateTime
DateTime
now()
xsd:base64Binary
ByteArray
Binary data
Complex types
Struct
{ field1: "value", field2: 123 }
Arrays/Lists
Array
["item1", "item2", "item3"]
Intelligent Casting
When xsi:type information is present in the SOAP response, BoxLang uses it to cast values accurately:
Without xsi:type, BoxLang attempts intelligent casting:
Complex Type Conversion
SOAP complex types are automatically converted to BoxLang structs:
Array Conversion
SOAP arrays are automatically converted to BoxLang arrays:
Response Unwrapping
Many SOAP services wrap the actual result in a container element. BoxLang automatically unwraps single-property structs:
This makes working with SOAP responses much more intuitive!
🔍 WSDL Discovery
BoxLang automatically parses WSDL documents and discovers:
Service endpoint - Where to send SOAP requests
Operations - Available methods you can call
Input parameters - Required and optional arguments for each operation
Output types - Return types and structure
SOAP version - 1.1 or 1.2 (auto-detected from binding)
Binding style - Document/literal, RPC/encoded, etc.
Example: Inspecting a Service
⚠️ Error Handling
SOAP faults are automatically converted to BoxLang exceptions:
SOAP Fault Structure
SOAP faults contain:
Fault Code - Standard SOAP fault code (Client, Server, etc.)
Fault String - Human-readable error message
Fault Detail - Additional error details from the service
BoxLang combines these into a single exception message for easy handling.
💡 Best Practices
SOAP Client Best Practices:
Cache clients - Create SOAP clients once and reuse them (WSDL parsing is expensive)
Set appropriate timeouts - SOAP calls can be slow, adjust timeouts accordingly
Handle faults gracefully - Always wrap SOAP calls in try-catch blocks
Inspect operations first - Use
getOperationNames()to see what's availableCheck operation info - Use
getOperationInfo()to understand parameters before callingUse named arguments - Struct arguments are clearer than positional arrays
Monitor statistics - Track invocations and failures with
getStatistics()Secure credentials - Never hardcode passwords, use environment variables or config files
Test with public WSDLs - Start with known working services for testing
Log SOAP requests - Enable debug logging to see raw SOAP XML for troubleshooting
Common Gotchas:
Case sensitivity - SOAP operation names are case-sensitive, match them exactly
Null values - Some SOAP services don't handle null/empty values well
Namespace issues - If operations aren't discovered, check WSDL namespace definitions
Authentication - Some services require API keys in headers, not just basic auth
SOAP version - Mismatched SOAP versions cause cryptic errors
Timeout defaults - Default timeout is 30 seconds, increase for slow services
🔧 Advanced Usage
Programmatic Operation Discovery
Build dynamic SOAP integrations by discovering operations at runtime:
Building a SOAP Service Wrapper
Create a reusable service class:
Handling Multiple Services
Manage multiple SOAP services efficiently:
📚 SOAP vs REST
While BoxLang excels at both SOAP and REST APIs, here's when to choose each:
Use SOAP When:
✅ Working with legacy enterprise systems
✅ Need WS-Security, WS-ReliableMessaging, or other WS-* standards
✅ Strict contracts and type safety required
✅ WSDL-based service discovery is beneficial
✅ Working with financial, healthcare, or government systems
Use REST (http() BIF) When:
✅ Building modern web APIs
✅ Need lightweight, fast communication
✅ JSON is preferred over XML
✅ Stateless operations are sufficient
✅ HTTP caching and standard methods are beneficial
🎯 Summary
BoxLang's SOAP support provides:
🔍 Automatic WSDL discovery - Parse and understand services automatically
🎯 Fluent invocation - Call SOAP operations like native BoxLang methods
🔄 Intelligent type conversion - SOAP XML types automatically become BoxLang types
📦 Smart unwrapping - Clean, intuitive response structures
🔒 Authentication support - HTTP Basic Auth built-in
⏱️ Configurable behavior - Timeouts, headers, SOAP versions
📊 Service inspection - Discover operations and parameters programmatically
⚠️ Error handling - SOAP faults become BoxLang exceptions
📈 Statistics tracking - Monitor usage and performance
Whether you're integrating with legacy enterprise systems, consuming third-party SOAP APIs, or building service-oriented architectures, BoxLang's SOAP client makes it simple and elegant.
Last updated
Was this helpful?
