Search code examples
xproc

XProc 3: form-data with file


does someone have an idea how to handle form-data with file upload in XProc 3.0?

<p:variable name="headers" as="map(xs:string, xs:string)" select="map {
        'authorization': concat('Bearer ', $api-key),
        'content-type': 'multipart/form-data'
        }"/>
       
    <p:variable name="method" select="'POST'"/>
    <p:variable name="endpoint" select="'/files'"/>
    <p:http-request name="http-request" parameters="map {'accept-multipart':true()}">
        <p:with-input>
            <!-- ??? -->
            <p:document href="input/deu.txt" content-type="text/plain"/>
        </p:with-input>
        <p:with-option name="href" select="escape-html-uri(concat($base-uri, $endpoint))"/>
        <p:with-option name="method" select="$method"/>
        <p:with-option name="headers" select="$headers"/>
    </p:http-request>

cURL snippet


curl https://... \
  -H "Authorization: Bearer $API_KEY" \
  -F purpose="mypurpose" \
  -F file="@mydata.txt"

I want to upload files via an API with XProc 3.0 and MorganaXProc-IIIse-1.


Solution

  • I don't think know whether MorganaXProc-IIIse imposes any restrictions on form data, but the XProc 3.0 spec requires XProc processors to handle multipart requests. If the content-type header begins with multipart/, a multipart request will be created.

    This pipeline includes a POST request with the content type multipart/form-data. I've used https://httpbin.org/post for testing. More examples for <p:http-request/> can be found in my XProc 3.0 Tutorial:

    <?xml version="1.0" encoding="UTF-8"?>
    <p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">
          
      <p:input port="source" content-types="text/plain">
        <p:document href="input/deu.txt"/>
      </p:input>
          
      <p:output port="result"/>
        
      <p:option name="api-key" values="'123456'"/>
          
      <p:http-request name="http-request"
                      headers="map{
                        'content-type':'multipart/form-data',
                        'api-key': concat('Bearer ', $api-key)
                        }"
                      method="post"
                      href="https://httpbin.org/post">
      </p:http-request>
    
    </p:declare-step>