Sample code for 30+ languages & platforms
Lua

Simple SOAP Request

See more HTTP Examples

Demonstrates how to send a simple SOAP request.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- --------------------------------------------------------------------------------
-- Also see Chilkat's Online WSDL Code Generator
-- to generate code and SOAP Request and Response XML for each operation in a WSDL.
-- --------------------------------------------------------------------------------

-- Create the following XML to be sent in the SOAP request body.

-- <soapenv:Envelope xmlns:dat="http://www.dataaccess.com/webservicesserver/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
--     <soapenv:Header/>
--     <soapenv:Body>
--         <dat:NumberToDollars>
--             <dat:dNum>99.0</dat:dNum>
--         </dat:NumberToDollars>
--     </soapenv:Body>
-- </soapenv:Envelope>

local xml = chilkat.Xml.new()
xml.tag = "soapenv:Envelope"
xml:add_attribute("xmlns:dat", "http://www.dataaccess.com/webservicesserver/")
xml:add_attribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/")
xml:update_child_content("soapenv:Header", "")
xml:update_child_content("soapenv:Body|dat:NumberToDollars|dat:dNum", "99.0")

-- In a SOAP HTTP request, including the XML declaration (<?xml version="1.0" encoding="UTF-8"?>) in the XML body is generally not required.
xml.emit_xml_decl = false
local soapRequestBody = xml:get_xml()

local endpoint = "https://www.dataaccess.com/webservicesserver/numberconversion.wso"
local soapAction = ""
--  For SOAP requests, the standard Content-Type is usually set to "text/xml" or "application/soap+xml"
local contentType = "text/xml"

local http = chilkat.Http.new()

http:clear_headers()
http:set_request_header("SOAPAction", soapAction)

local resp = chilkat.HttpResponse.new()
local success = http:http_str("POST", endpoint, soapRequestBody, "utf-8", contentType, resp)
if not success then
    print(http.last_error_text)
    return
end

-- Get the XML response body.
local responseXml = chilkat.Xml.new()
resp:get_body_xml(responseXml)

local statusCode = resp.status_code
print("response status code: " .. statusCode)

-- If the status code does not indicate succcess, then show the response XML,
-- which probably contains error information.
if statusCode ~= 200 then
    print(responseXml:get_xml())
    return
end

print(responseXml:get_xml())

-- Parse the successful SOAP response XML.

-- This is a sample of the response XML, but the namespace prefixes will be different.
-- We can parse the result using "*" for the namespace prefixes (see below).

-- <soapenv:Envelope xmlns:dat="http://www.dataaccess.com/webservicesserver/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
--     <soapenv:Header/>
--     <soapenv:Body>
--         <dat:NumberToDollarsResponse>
--             <dat:NumberToDollarsResult>string</dat:NumberToDollarsResult>
--         </dat:NumberToDollarsResponse>
--     </soapenv:Body>
-- </soapenv:Envelope>

local dat_NumberToDollarsResult = responseXml:get_child_content("*:Body|*:NumberToDollarsResponse|*:NumberToDollarsResult")
print("result: " .. dat_NumberToDollarsResult)