Sample code for 30+ languages & platforms
Rust

Simple SOAP Request

See more HTTP Examples

Demonstrates how to send a simple SOAP request.

Chilkat Rust Downloads

Rust

// --------------------------------------------------------------------------------
// 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>

let xml = chilkat::Xml::new();
xml.set_tag("soapenv:Envelope");
let _ = xml.add_attribute("xmlns:dat", "http://www.dataaccess.com/webservicesserver/");
let _ = 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.set_emit_xml_decl(false);
let soap_request_body = xml.get_xml().unwrap_or_default();

let endpoint = "https://www.dataaccess.com/webservicesserver/numberconversion.wso".to_string();
let soap_action = String::new();
//  For SOAP requests, the standard Content-Type is usually set to "text/xml" or "application/soap+xml"
let content_type = "text/xml".to_string();

let http = chilkat::Http::new();

http.clear_headers();
http.set_request_header("SOAPAction", &soap_action);

let resp = chilkat::HttpResponse::new();
if http.http_str("POST", &endpoint, &soap_request_body, "utf-8", &content_type, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

// Get the XML response body.
let response_xml = chilkat::Xml::new();
let _ = resp.get_body_xml(&response_xml);

let status_code = resp.status_code();
println!("response status code: {}", status_code);

// If the status code does not indicate succcess, then show the response XML,
// which probably contains error information.
if status_code != 200 {
    println!("{}", response_xml.get_xml().unwrap_or_default());
    return;
}

println!("{}", response_xml.get_xml().unwrap_or_default());

// 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>

let dat_number_to_dollars_result = response_xml.get_child_content("*:Body|*:NumberToDollarsResponse|*:NumberToDollarsResult").unwrap_or_default();
println!("result: {}", dat_number_to_dollars_result);