Search code examples
grailsgrails-plugingrails-2.0

RestBuilder Plugin. How can I upload a file without creating a file?


Currently, I can upload files(exist) with Grails's RestBuilder. However, I want to upload a file without creating a file . I want to create binary data (= Text File) in a program and send it directly Is it possible?

RestBuilder rest = new RestBuilder()
RestResponse resp = rest.post(url){
    contentType("multipart/form-data")
        setProperty("dataFile",[filePath])// <- it can
        setProperty("dataFile",[ byte[] or inputStream() or String ? ])// <- Is it possible?
}

'''

Solution

  • I'm sure you figured this out already, but you can just use a String reference or a byte[] just as you can use File instances for the multipart request using RestBuilder. It should 'just work' e.g.

    RestBuilder rest = new RestBuilder()
    RestResponse response = rest.post(url) {
        contentType 'multipart/form-data'
        stringPart = 'hello' // String
        bytePart = '68656c6c6f'.decode64() // byte[]
        filePart = new File('/path/to/file.jpg') // File
    }