[groovy-user] File upload with HTTPBuilder

1,121 views
Skip to first unread message

Martin Jones

unread,
Sep 15, 2010, 6:50:10 AM9/15/10
to us...@groovy.codehaus.org
Hi,

I'm trying to use Groovy HTTPBuilder to submit a POST request to the
cgi script described here:

http://itol.embl.de/help/batch_help.shtml

And am having a bit of trouble. I've not been able to find a file
upload example, and I'm not very familiar with this kind of thing. How
to I include a file in the post body Map? So far I have a fairly naive
approach:


def treeFile = new File('/home/martin/Desktop/gk/manual.nex.con')

def http = new HTTPBuilder( 'http://itol.embl.de/' )

def postBody = [
treeFile:treeFile,
treeFormat:'nexus',
treeName:'testtree',
]

http.post( path: 'batch_uploader.cgi', body: postBody,
requestContentType: URLENC ) { resp, reader ->

println "response status: ${resp.statusLine}"
System.out << reader // print response stream
}

Any help appreciated.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Thom Nichols

unread,
Sep 15, 2010, 7:55:13 AM9/15/10
to user
So HTTP file upload is not as straightforward as it may seem.  There's a special RFC for how to encode the POST body as mime-multipart so it's not as easy as URL-encoding key/value pairs.  Here's some background: http://www.vivtek.com/rfc1867.html

More to the point, there are examples of Multipart upload for HttpClient 4 that you should be able to use.  Here's an example:
http://radomirml.com/2009/02/13/file-upload-with-httpcomponents-successor-of-commons-httpclient

In your case, you can construct the MultipartEntity and set it on the request in HTTPBuilder's request config closure like so:

http.request( GET, HTML ) { req ->
  def postBody = new MultipartEntity(...)
  ....
  req.entity = postBody

  response.success = { ... }
}

Please let me know how this works out fr you as I've never tried it.  As this seems like a relatively common task, I'd consider adding built-in support, say, to detect a file in the request parameters (like your example) and use a MultipartEntity automatically.  But on the same token, you're the first person who has asked about it :)  Scratch that, I found someone asked the question last year:
http://groovy.329449.n5.nabble.com/forum/PostLink.jtp?post=347727
They got it working through a similar mechanism; basically by using a custom Entity class.

But I'd bet either of these methods work without too much extra effort.  I'll open a JIRA enhancement to support multipart upload and see if I ever get around to implementing it :)

Either way, let me know how it works out for you.

-Tom

ysb33r

unread,
Apr 27, 2014, 4:44:55 PM4/27/14
to us...@groovy.codehaus.org
Reviving an old thread ...

Was the ability to upload files ever added to v 7.2+ or 8.0-SNAPSHOT?

--
Schalk





--
View this message in context: http://groovy.329449.n5.nabble.com/File-upload-with-HTTPBuilder-tp2840433p5719404.html
Sent from the groovy - user mailing list archive at Nabble.com.

Schalk Cronj é

unread,
Apr 27, 2014, 7:59:40 PM4/27/14
to us...@groovy.codehaus.org
To answer my own question, it works with file uploads. If you pass

requestContentType : BINARY,
body : content.bytes

Where content is a File instance.
--
Schalk W. Cronjé
@ysb33r
Reply all
Reply to author
Forward
0 new messages