我想模拟一个需要将一些输入/文本字段与文件数据组合的HTTP POST请求。看起来我只能有其中之一,而不能两个都有?
在下面的代码片段中,paramsToPost = [name: 'John', age:22]
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0')
Boolean doHttpPost(String url, Map paramsToPost, String fileContent) {
HTTPBuilder http = new HTTPBuilder(url)
def resp = http.request(Method.POST ) { req ->
MultipartEntity mpe = new MultipartEntity()
mpe.addPart "foo", new StringBody(fileContent)
req.entity = mpe
// body = paramsToPost // no such property
}
println "response: ${resp}"
return true
}
有没有现成的样例可以提供?