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

如何在Groovy HTTPBuilder中正确传递multipart/form-data表单字段?

问题描述

需要编写一个等效于以下cURL命令的Groovy脚本:

curl -X 'POST' 'url/authenticate' -H 'accept: */*' -H 'Content-Type: multipart/form-data' -F 'Username=user_name' -F 'Password=pass_word'

我编写了如下脚本,但请求返回400响应码,提示“Password字段是必填项”和“Username字段是必填项”:

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.Method

def http = new HTTPBuilder('url/authenticate')

def body = [
    "Username": 'user_name',
    "Password": 'pass_word'
]

http.request(Method.POST, ContentType.URLENC) {
    headers.'Accept' = '*/*'
    body = body
    response.success = { resp, reader ->
        log.warn("Response Status: ${resp.statusLine}")
        log.warn("Response Data: ${reader}")
    }
    response.failure = { resp, reader ->
        log.warn("Error Response Status: ${resp.statusLine}")
        log.warn("Error Response Data: ${reader}")
    }
}

补充说明:在Postman中传递这些表单字段的POST请求可正常运行。请问如何在此脚本中正确传递表单字段?


解决方案

问题核心是请求内容类型不匹配:原cURL和Postman使用的是multipart/form-data类型,但你的Groovy脚本用了ContentType.URLENC(对应application/x-www-form-urlencoded),服务端无法识别该格式的表单字段,因此返回必填项缺失的错误。

方案一:基于Apache HttpClient的传统写法

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.Method
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.mime.content.StringBody

def http = new HTTPBuilder('url/authenticate')

http.request(Method.POST) {
    headers.'Accept' = '*/*'
    // 构造multipart/form-data格式的请求实体
    request.entity = MultipartEntityBuilder.create()
        .addPart('Username', new StringBody('user_name', ContentType.TEXT_PLAIN))
        .addPart('Password', new StringBody('pass_word', ContentType.TEXT_PLAIN))
        .build()

    response.success = { resp, reader ->
        log.warn("Response Status: ${resp.statusLine}")
        log.warn("Response Data: ${reader}")
    }
    response.failure = { resp, reader ->
        log.warn("Error Response Status: ${resp.statusLine}")
        log.warn("Error Response Data: ${reader}")
    }
}

关键修改点:

  • 移除ContentType.URLENC参数,改用MultipartEntityBuilder手动构造multipart/form-data类型的请求体
  • 通过addPart方法添加表单字段,每个字段对应一个StringBody实例
  • 请求头的Content-Type会由MultipartEntityBuilder自动设置,无需手动指定

方案二:新版本HTTPBuilder简化写法(基于OkHttp)

如果使用的是较新版本的HTTPBuilder(如基于OkHttp的分支),可以用更简洁的写法:

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.MULTIPART_FORM_DATA

def http = new HTTPBuilder('url/authenticate')

http.post(contentType: MULTIPART_FORM_DATA) {
    headers.Accept = '*/*'
    body = [
        Username: 'user_name',
        Password: 'pass_word'
    ]

    response.success = { resp, reader ->
        log.warn("Response Status: ${resp.statusLine}")
        log.warn("Response Data: ${reader}")
    }
    response.failure = { resp, reader ->
        log.warn("Error Response Status: ${resp.statusLine}")
        log.warn("Error Response Data: ${reader}")
    }
}

这种写法直接指定contentType: MULTIPART_FORM_DATA,传入表单键值对即可,底层会自动处理multipart格式的构造。


内容的提问来源于stack exchange,提问作者lchnkv av

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 05:06:04