Skip to content

Instantly share code, notes, and snippets.

@kouzouigh
Created July 1, 2019 06:55
Show Gist options
  • Save kouzouigh/384ea8313896d372ead0d74dfe439275 to your computer and use it in GitHub Desktop.
Save kouzouigh/384ea8313896d372ead0d74dfe439275 to your computer and use it in GitHub Desktop.
Groovy HTTPBuilder with multipart/form-data
import groovy.json.JsonSlurper
import groovyx.net.http.HTTPBuilder
import org.apache.http.entity.mime.MultipartEntityBuilder
import static groovyx.net.http.Method.POST
def url = '<URL>'
def token = '<TOKEN>'
def document = "<PATH_TO_FILE>"
def printErr = System.err.&println
http.request(POST) { req ->
uri.path = "/v2/upload"
headers.'Authorization' = token
requestContentType = 'multipart/form-data'
def entity = MultipartEntityBuilder
.create()
.addTextBody('type', 'driving_licence')
.addTextBody('side', 'front')
.addBinaryBody('file', new File(document), org.apache.http.entity.ContentType.IMAGE_PNG, 'driving-licence')
.build()
req.entity = entity
response.failure = { resp ->
printErr "Document '${document}' upload fail: ${resp.statusLine.statusCode}, message: ${resp.responseBase.entity.content.text}"
System.exit(-1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment