httppostgroovymultipartform-datahttpbuilder

HTTPBuilder and MultipartEntity / multipart form-data in Groovy


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?


Solution

  • 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.