jgritman / httpbuilder

314 stars 154 forks source link

multipart/form-data POST is possible with HTTPBuilder? #52

Open ppazos opened 8 years ago

ppazos commented 8 years ago

I need to do multipart/form-data POSTs instead of URLENC but can't find any docs about it. Is it possible? Thanks.

gregorydickson commented 8 years ago

In the wiki there are some basic HTTP POST examples: https://github.com/jgritman/httpbuilder/wiki/POST-Examples

jonnybot0 commented 7 years ago

Yeah, but there's no good guidance on how to post to APIs that don't accept URL Encoded data. There's an old SO post about it at https://stackoverflow.com/questions/6452678/httpbuilder-and-multipartentity-multipart-form-data-in-groovy.

ferbueno commented 7 years ago

Did any of you guys solve this? I've been struggling with the same thing

jonnybot0 commented 7 years ago

So, the best answer I've found has been to simply move to the better maintained http-builder-ng library. See https://github.com/http-builder-ng/http-builder-ng. It has good MultiPart support out of the box. https://http-builder-ng.github.io/http-builder-ng/asciidoc/html5/#_multipart

ferbueno commented 7 years ago

Thank you very much @jonnybot0! Worked like a charm!

ppazos commented 6 years ago

@gregorydickson all examples are for URLENC not multipart.

jonnybot0 commented 1 year ago

So, I stumbled across this, saw my old comment, and realized that this is perfectly doable using ye olde HTTPBuilder.

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

def file = new File('myFile.png')
def httpBuilder = new HttpBuilder('http://whatever.io')
httpBuilder.request(Method.POST, ContentType.ANY) {
    request.entity = MultipartEntityBuilder.create()
        .addPart('file', new FileBody(file))
        .build()
}

Not sure it will work in every single case, but it will in many.