You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在XProc 3.0中处理含文件上传的form-data请求?

解决XProc 3.0(MorganaXProc-IIIse)的multipart/form-data文件上传问题

针对你的场景,要实现对应cURL的-F表单上传,需要调整p:http-request的输入结构和配置,以下是可行方案:

关键调整点

  • 不要手动设置content-type: multipart/form-data:MorganaXProc-IIIse会自动生成带正确边界的Content-Type头,手动设置会导致边界缺失,请求无效。
  • 启用发送multipart的参数:将parameters中的accept-multipart改为send-multipart:true(),这是触发multipart请求发送的关键配置。
  • 构造每个表单部分:每个-F对应的表单字段(包括文件)都要作为单独的输入项,并通过content-disposition属性指定表单字段名、文件名(针对文件)。

修改后的完整代码示例

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">
    <p:variable name="api-key" select="'你的API密钥'"/>
    <p:variable name="base-uri" select="'https://目标API地址'"/>

    <p:variable name="headers" as="map(xs:string, xs:string)" select="map {
            'authorization': concat('Bearer ', $api-key)
        }"/>
       
    <p:variable name="method" select="'POST'"/>
    <p:variable name="endpoint" select="'/files'"/>
    
    <p:http-request name="http-request" parameters="map {'send-multipart':true()}">
        <p:with-input>
            <!-- 对应curl的 -F purpose="mypurpose" -->
            <p:inline content-type="text/plain" content-disposition="form-data; name=&quot;purpose&quot;">
                mypurpose
            </p:inline>
            <!-- 对应curl的 -F file="@mydata.txt" -->
            <p:document href="input/deu.txt" 
                        content-type="text/plain"
                        content-disposition="form-data; name=&quot;file&quot;; filename=&quot;deu.txt&quot;"/>
        </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>
</p:declare-step>

代码说明

  1. 表单字段purpose:用<p:inline>构造文本型表单字段,通过content-disposition指定name="purpose",对应cURL的-F purpose="mypurpose"。
  2. 文件字段file:用<p:document>加载本地文件,content-disposition同时指定name="file"(表单字段名)和filename="deu.txt"(上传时的文件名),对应cURL的-F file="@mydata.txt"。
  3. send-multipart参数:告诉p:http-request将输入的多个文档打包为multipart/form-data格式发送,这是实现上传的核心配置。

验证要点

  • 确保$api-key和$base-uri变量已正确赋值。
  • 确认input/deu.txt路径相对于XProc脚本的位置正确。

内容的提问来源于stack exchange,提问作者Roland Dreger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 14:47:34