Sample code for 30+ languages & platforms
Java

Send SOAP multipart/related with XML Body and Zip Attachment

See more REST Misc Examples

Demonstrates how to construct and send a multipart/related POST containing a SOAP XML body and a binary zip attachment.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // This example constructs and sends the following HTTP request.
        // (The boundary strings will be different and auto-generated by Chilkat)

        // Accept-Encoding: gzip,deflate
        // SOAPAction: invio
        // Content-Type: Multipart/Related; boundary="MIME_boundary"; type="text/xml"; start="mailto:rootpart@soapui.org"
        // MIME-Version: 1.0
        //
        // --MIME_boundary
        // Content-Type: text/xml; charset=UTF-8
        // Content-Transfer-Encoding: 8bit
        // Content-ID: <rootpart@soapui.org>
        //
        // <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:attachment.ws.it">
        // <soapenv:Header/>
        // <soapenv:Body>
        //   <urn:inputBean>
        //    <nomeFileAllegato>myfile.zip</nomeFileAllegato>
        //   </urn:inputBean>
        // </soapenv:Body>
        // </soapenv:Envelope>
        //
        // --MIME_boundary
        // Content-Type: application/zip
        // Content-Transfer-Encoding: binary
        // Content-ID: <myfile.zip>
        // Content-Disposition: attachment; name="myfile.zip"; filename="myfile.zip"
        //
        // ... binary zip data goes here ...
        // --MIME_boundary--
        //

        Rest rest = new Rest();

        rest.addHeader("Accept-Encoding", "gzip,deflate");
        rest.addHeader("SOAPAction", "invio");
        rest.addHeader("Content-Type", "Multipart/Related; boundary=\"MIME_boundary\"; type=\"text/xml\"; start=\"mailto:rootpart@soapui.org\"");
        rest.addHeader("MIME-Version", "1.0");

        // Build the SOAP XML:
        // Use this online tool to generate the code from sample XML:
        // Generate Code to Create XML

        Xml xml = new Xml();
        xml.setTag("soapenv:Envelope");
        xml.addAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        xml.addAttribute("xmlns:urn", "urn:attachment.ws.it");
        xml.updateChildContent("soapenv:Header", "");
        xml.updateChildContent("soapenv:Body|urn:inputBean|nomeFileAllegato", "myfile.zip");

        // Build the SOAP XML sub-part
        rest.setPartSelector("1");
        rest.addHeader("Content-Type", "text/xml; charset=UTF-8");
        rest.addHeader("Content-Transfer-Encoding", "8bit");
        rest.addHeader("Content-ID", "<rootpart@soapui.org>");
        rest.setMultipartBodyString(xml.getXml());

        // Build the sub-part containing the .zip
        rest.setPartSelector("2");
        rest.addHeader("Content-Type", "application/zip");
        rest.addHeader("Content-Transfer-Encoding", "binary");
        rest.addHeader("Content-ID", "<myfile.zip>");
        rest.addHeader("Content-Disposition", "attachment; name=\"myfile.zip\"; filename=\"myfile.zip\"");

        // Add a zip file to the 2nd sub-part body.
        // This example will load the .zip file into memory, but it is also possible to stream directly from the file as the request is sent
        // by calling SetMultipartBodyStream.
        BinData bd = new BinData();
        bd.loadFile("qa_data/zips/helloWorld.zip");
        rest.setMultipartBodyBd(bd);

        // To be safe, always revert the PartSelector back to 0..
        rest.setPartSelector("0");

        // Send the request by connecting to the web server and then sending..
        // Connect using TLS.
        boolean bAutoReconnect = true;
        String responseStr;
        try {
            rest.connect("www.chilkatsoft.com", 443, true, bAutoReconnect);

            // In this example, we're setting DebugMode.
            // When debug mode is turned on, no request is actually sent.
            // Instead, the request that would be sent is recorded to memory
            // and we can retrieve it afterwards..
            rest.setDebugMode(true);

            responseStr = rest.fullRequestMultipart("POST", "/someTargetPath");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Retrieve the request that would've been sent and save it to a file.
        // We can then examine the file to see if the request was structured as we desired.
        // (Note: The zip bytes will be binary data and will appear as garbled garbage in a text editor..)
        BinData bdRequest = new BinData();
        rest.getLastDebugRequest(bdRequest);
        bdRequest.writeFile("qa_output/multipartRelatedRequest.bin");

        System.out.println("OK, examine the output file..");
    }
}