Created
July 1, 2019 06:55
-
-
Save kouzouigh/384ea8313896d372ead0d74dfe439275 to your computer and use it in GitHub Desktop.
Groovy HTTPBuilder with multipart/form-data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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