Sample code for 30+ languages & platforms
Rust

Yet Another SOAP MTOM POST Example

See more HTTP Misc Examples

Demonstrates how to sending a SOAP request with an MTOM/XOP attachment...

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

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

// First build the following XML:

// 	<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
// 	    xmlns:imp="http://www.yourdomain.it/ImportMultiploWorkflowPAWS/">
// 	   <soapenv:Header/>
// 	   <soapenv:Body>
// 	      <imp:InputImportMultiploWorkflowPA>
// 	         <usernameEis>MyUsername</usernameEis>
// 	         <passwordEis>MyPassword</passwordEis>
// 	         <postazioneEis>COSO00</postazioneEis>
// 	         <istitutoEis>COSO</istitutoEis>
// 	         <codiceAzienda>COSO0000</codiceAzienda>
// 	         <fileZip><inc:Include href="cid:780931946797" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></fileZip>
// 	         <fileZipName>IT04769180151_00044.zip</fileZipName>
// 	         <workflow>carica</workflow>
// 	      </imp:InputImportMultiploWorkflowPA>
// 	   </soapenv:Body>
// 	</soapenv:Envelope>

let xml = chilkat::Xml::new();
xml.set_tag("soapenv:Envelope");
let _ = xml.add_attribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
let _ = xml.add_attribute("xmlns:imp", "http://www.yourdomain.it/ImportMultiploWorkflowPAWS/");
xml.update_child_content("soapenv:Header", "");
xml.update_child_content("soapenv:Body|imp:InputImportMultiploWorkflowPA|usernameEis", "MyUsername");
xml.update_child_content("soapenv:Body|imp:InputImportMultiploWorkflowPA|passwordEis", "MyPassword");
xml.update_child_content("soapenv:Body|imp:InputImportMultiploWorkflowPA|postazioneEis", "COSO00");
xml.update_child_content("soapenv:Body|imp:InputImportMultiploWorkflowPA|istitutoEis", "COSO");
xml.update_child_content("soapenv:Body|imp:InputImportMultiploWorkflowPA|codiceAzienda", "COSO0000");
let _ = xml.update_attr_at("soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZip|inc:Include", true, "href", "cid:780931946797");
let _ = xml.update_attr_at("soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZip|inc:Include", true, "xmlns:inc", "http://www.w3.org/2004/08/xop/include");
xml.update_child_content("soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZipName", "IT04769180151_00044.zip");
xml.update_child_content("soapenv:Body|imp:InputImportMultiploWorkflowPA|workflow", "carica");

// We want our top-level HTTP request header to look like this:

// POST /http-router-softwarehubsystem/webservices/importMultiploWorkflowPA HTTP/1.1
// Accept-Encoding: gzip,deflate
// Content-Type: multipart/related; type="application/xop+xml"; start="<rootpart@soapui.org>"; start-info="text/xml"; boundary="----=_Part_0_1353857996.1546013984933"
// SOAPAction: "http://www.yourdomain.it/ImportMultiploWorkflowPAWS/importMultiploWorkflowPA"
// MIME-Version: 1.0
// Content-Length: <chilkat will add this header>
// Host: <chilkat will add this header>
// Connection: Keep-Alive
// User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/http-router-softwarehubsystem/webservices/importMultiploWorkflowPA");
req.set_content_type("multipart/related; type=\"application/xop+xml\"; start=\"<rootpart@soapui.org>\"; start-info=\"text/xml\"");
req.add_header("SOAPAction", "\"http://www.yourdomain.it/ImportMultiploWorkflowPAWS/importMultiploWorkflowPA\"");
req.add_header("Accept-Encoding", "gzip,deflate");
req.add_header("Mime-Version", "1.0");
req.add_header("Connection", "Keep-Alive");
req.add_header("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)");

// Add the XML content for the 1st MIME sub-part.
xml.set_emit_xml_decl(false);
xml.set_emit_compact(false);
let _ = req.add_string_for_upload2("", "", &xml.get_xml().unwrap_or_default(), "utf-8", "application/xop+xml; charset=UTF-8; type=\"text/xml\"").is_ok();

// We want the 1st sub-header (the sub-part containing the XML) to look like this:

// Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
// Content-Transfer-Encoding: 8bit
// Content-ID: <rootpart@soapui.org>

// Make sure we set sub-headers *after* calling AddStringForUpload2, otherwise AddStringForUpload2 might change these explicitly set headers.
let _ = req.add_sub_header(0, "Content-Transfer-Encoding", "8bit").is_ok();
let _ = req.add_sub_header(0, "Content-ID", "<rootpart@soapui.org>").is_ok();

// Add a reference to the local file for the file data for the 2nd MIME sub-part.
let _ = req.add_file_for_upload2("", "qa_data/zips/helloWorld.zip", "application/zip; name=IT04769180151_00044.zip").is_ok();

// We want the 2nd sub-header (for the MIME sub-part containing a .zip file) to look like this:
// Content-Type: application/zip; name=IT04769180151_00044.zip
// Content-Transfer-Encoding: binary
// Content-ID: <IT04769180151_00044.zip>
// Content-Disposition: attachment; name="IT04769180151_00044.zip"; filename="IT04769180151_00044.zip"

// Make sure we set sub-headers *after* calling AddFileForUpload2, otherwise AddFileForUpload2 might change these explicitly set headers.
let _ = req.add_sub_header(1, "Content-Transfer-Encoding", "binary").is_ok();
let _ = req.add_sub_header(1, "Content-ID", "<IT04769180151_00044.zip>").is_ok();
let _ = req.add_sub_header(1, "Content-Disposition", "attachment; name=\"IT04769180151_00044.zip\"; filename=\"IT04769180151_00044.zip\"").is_ok();

// Keep a session log so we can examine the exact request/response.
// Remove this line when finished with debugging.
http.set_session_log_filename("qa_output/mtom_sessionLog.txt");

let use_tls = false;
// Note: Please don't run this example without changing the domain to your own domain...
let resp = chilkat::HttpResponse::new();
if http.http_s_req("www.yourdomain.it", 80, use_tls, &req, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let xml_response = chilkat::Xml::new();
let _ = xml_response.load_xml(&resp.body_str()).is_ok();
// println xmlResponse.GetXml();

println!("OK.");