Open ppazos opened 8 years ago
In the wiki there are some basic HTTP POST examples: https://github.com/jgritman/httpbuilder/wiki/POST-Examples
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.
Did any of you guys solve this? I've been struggling with the same thing
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
Thank you very much @jonnybot0! Worked like a charm!
@gregorydickson all examples are for URLENC not multipart.
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.
I need to do multipart/form-data POSTs instead of URLENC but can't find any docs about it. Is it possible? Thanks.