Groovy HTTPBuilder post file upload without multipart/form-data?

user3915521 picture user3915521 · Aug 6, 2014 · Viewed 8.5k times · Source

How do I perform an HTTP post in groovy using HTTPBuilder that uploads the raw bytes of a file without using multipart/form-data? Specifically, I want my request to look like this:

POST http://....
Host: myhost
Content-Length: numBytes
Proxy-Connection: Keep-Alive

Raw Data

Answer

cfrick picture cfrick · Aug 7, 2014

you could send it as binary

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
def http = new HTTPBuilder('http://localhost:8080')
http.post(path:'/', body: new File('/etc/passwd').bytes, requestContentType: BINARY) { response ->
    println response.statusLine
}