Uploading a file with HTTP PUT in Groovy with Basic Auth


dependencies {
   compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
   compile 'org.apache.httpcomponents:httpmime:4.5'


import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import org.apache.http.client.entity.EntityBuilder
import org.apache.http.util.EntityUtils

class HttpPut {
   File inputFile
   String url
   String username
   String password

   HttpPut withFile(File inputFile) {
       this.inputFile = inputFile
       return this

   HttpPut withUrl(String url) {
       this.url = url
       return this

   HttpPut withCredentials(String username, String password) {
       this.username  = username
       this.password = password
       return this

   void put() {
       def http = new HTTPBuilder(url)

       http.auth.basic(username, password)

       http.request(Method.PUT) { request ->
           request.entity = EntityBuilder.create().setBinary(inputFile.bytes).build()

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s