GroBuy中的HTTPBuilder和MultipartEntity / multipart表单数据 [英] HTTPBuilder and MultipartEntity / multipart form-data in Groovy

查看:171
本文介绍了GroBuy中的HTTPBuilder和MultipartEntity / multipart表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试模拟需要将某些INPUT / TEXT字段与来自文件的数据组合在一起的HTTP POST。它看起来像我可以有一个或另一个,但不是两个?



在下面的代码段中,paramsToPost = [name:'John',age:22] p>

  @Grab(group ='org.codehaus.groovy.modules.http-builder',module ='http-builder',version ='0.5.0')
布尔值doHttpPost(String url,Map paramsToPost,String fileContent){
HTTPBuilder http = new HTTPBuilder(url)
def resp = http.request(Method.POST ){req - >
MultipartEntity mpe = new MultipartEntity()
mpe.addPartfoo,new StringBody(fileContent)
req.entity = mpe

// body = paramsToPost / / no such property
}

printlnresponse:$ {resp}

return true
}

任何人都有工作示例吗?

解决方案

<对于其他人在寻找答案,请使用HTTPBuilder的这个分支。



https: //github.com/berngp/httpbuilder/tree/branch%2Fadd%2FMultiPart-Form



在某些时候,我希望这会被合并进入主分店。


Trying to simulate a HTTP POST that needs to combine some INPUT/TEXT fields along with data from a file. It looks like I can have one or the other, but not both?

In the snippet below, 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
}

Anybody have a working sample please?

解决方案

For anybody else looking for an answer, use this fork of the HTTPBuilder.

https://github.com/berngp/httpbuilder/tree/branch%2Fadd%2FMultiPart-Form

At some point, I'd expect this to be merged into the main branch.

这篇关于GroBuy中的HTTPBuilder和MultipartEntity / multipart表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆