Created
February 27, 2021 10:51
-
-
Save bitsnaps/18fbd1b26e6db8f61bafd1db34ff17dd to your computer and use it in GitHub Desktop.
Upload a file to LogicalDOC DMS using a REST post request
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
@Grapes([ | |
@Grab('org.slf4j:slf4j-simple:1.7.25'), | |
@Grab('io.github.http-builder-ng:http-builder-ng-okhttp:1.0.4') | |
]) | |
import groovyx.net.http.OkHttpBuilder | |
import groovyx.net.http.OkHttpEncoders | |
import groovyx.net.http.MultipartContent | |
import static groovyx.net.http.HttpBuilder.configure | |
String BASE_URL = '[HOST_ENDPOINT]' | |
String URI_DOC_UPLOAD = '/logicaldoc/services/rest/document/upload' | |
// Get list of folders | |
def folders = OkHttpBuilder.configure { | |
request.uri = "${BASE_URL}/logicaldoc/services/rest/folder/listChildren?folderId=4" | |
request.auth.basic 'admin', 'admin' | |
request.contentType = 'application/json' | |
}.get() | |
println(folders) | |
File docFile = new File('/full/path/to/doc/document.pdf') | |
// Upload a file (okhttp) | |
def text = OkHttpBuilder.configure { | |
request.uri = BASE_URL | |
request.auth.basic '[USERNAME]', '[PASSWORD]' | |
// request.encoder('application/pdf'){ ChainedHttpConfig config, ToServer req-> | |
// req.toServer(new FileInputStream(docFile)) | |
// } | |
}.post { | |
request.uri.path = URI_DOC_UPLOAD | |
request.contentType = 'multipart/form-data' | |
request.body = MultipartContent.multipart { | |
field 'folderId', '4' | |
field 'filename', docFile.name | |
field 'language', 'en' | |
part 'filedata', 'filename', 'application/octet-stream', docFile | |
} | |
request.encoder 'multipart/form-data', OkHttpEncoders.&multipart | |
} | |
println(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment