Search code examples
xmloxygenxmlxproc

How to sent a multipart/form-data POST request with XProc's p:http-request


I'd like to send a document using a multipart/form-data POST request with XProc's p:http-request function; Multi-part is needed as there are a few form fields that need to be sent too. I just can't get the content of the file (an XSD file in my case) into to request.

Any idea how to get this implemented?

Using xmlcalabash in OxygenXML v23.1

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0">

    <p:input port="source" primary="true">
        <p:document href="my.xsd"/>
    </p:input>
    <p:output port="result" primary="true"/>

    <p:http-request>
        <p:input port="source">
            <p:inline>
                <c:request href="https://myserver" method="post">
                    <c:multipart content-type="multipart/form-data" boundary="----12345678">
                        <c:body content-type="application/xml" disposition='form-data; name="xsd"; filename="myfile.xsd"'>
                        XXXX what do do here XXXX
                        </c:body>
                        <c:body content-type="plain/text" disposition='form-data; name="arg1"'>4</c:body>
                        <c:body content-type="plain/text" disposition='form-data; name="arg2"'>on</c:body>
               
                    </c:multipart>
                </c:request>
            </p:inline>
        </p:input>
    </p:http-request>

</p:declare-step>

Solution

  • You probably want to use the p:template step to insert the XML content into the HTTP request body.

    <p:declare-step xmlns:p="http://www.w3.org/ns/xproc" xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0">
        <p:output port="result" primary="true"/>
        <p:template>
            <p:input port="parameters"><p:empty/></p:input>
            <p:input port="source">
                <p:document href="my.xsd"/>
            </p:input>
            <p:input port="template">
                <p:inline>
                    <c:request href="https://myserver" method="post">
                        <c:multipart content-type="multipart/form-data" boundary="----12345678">
                            <c:body content-type="application/xml" disposition='form-data; name="xsd"; filename="myfile.xsd"'>
                            {/*}<!-- reference to the root element of the 'source' doc -->
                            </c:body>
                            <c:body content-type="plain/text" disposition='form-data; name="arg1"'>4</c:body>
                            <c:body content-type="plain/text" disposition='form-data; name="arg2"'>on</c:body>              
                        </c:multipart>
                    </c:request>
                </p:inline>
            </p:input>
        </p:template>
        <p:http-request/>
    </p:declare-step>
    

    See https://www.w3.org/TR/xproc-template/