Realise this is an oldy but might help others, although the question answers it from a beginner point of view it is difficult to fully understand how to reuse all of above properly.
Firstly the last comment on the question points to this link :
Which attempts to re-use the answer incorrectly. It has mixed above answer with an answer from this link
def content1 = new ContentDisposition("filename=aa.json")
def json1 = new File("resources/aa.json")
def attachments1 = new Attachment("root", new ByteArrayInputStream(json1.getBytes()), content1)
InputStream is2 = getClass().getResourceAsStream("resources/aa.json");
InputStream is1 = getClass().getResourceAsStream("resources/img.png");
ContentDisposition content2 = new ContentDisposition("attachment;filename=img.png")
Attachment attachments2 = new Attachment("root1", is1, content2)
def attachments = [attachments1, attachments2]
def body1 = new MultipartBody(attachments)
def client = new RESTClient( "https://somehost.com" )
ocutag.encoder.putAt(MediaType.MULTIPART_FORM_DATA, new MethodClosure(this, 'encodeMultiPart1'))
ocutag.encoder.putAt(MediaType.MULTIPART_FORM_DATA, new MethodClosure(this, 'encodeMultiPart2'))
The above is never going to work, I have it working like so:
def http = new RESTClient('http://localhost:8080')
http.encoder.putAt(MediaType.MULTIPART_FORM_DATA, new MethodClosure(this, 'encodeMultiPart'))
def body1 = new MultipartBody() //This is that MultipartBody class in the first answer example not the one from your imports......
body1.file=file.getInputStream()
body1.filename=file.name
def response = http.put( path: url, body:body1, query:['query':action, ], requestContentType: 'multipart/form-data' )
You also have encodeMultiPart2 and encodeMultiPart1, I think this is a misunderstanding just reuse 1 declaration of this method in both cases.. you don't need to do none of the attachments etc you have in your example..