{"id":6796,"date":"2012-08-31T12:25:15","date_gmt":"2012-08-31T06:55:15","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6796"},"modified":"2012-08-31T15:05:05","modified_gmt":"2012-08-31T09:35:05","slug":"groovy-http-builder-for-sending-multipart-file","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-http-builder-for-sending-multipart-file\/","title":{"rendered":"Groovy HTTP builder for sending multipart file."},"content":{"rendered":"<p>I had two applications that communicate with each other through web-services. There I had a requirement to send <strong>multi-part file<\/strong> from one application to another. After searching I came out with the following solution and thought to share:<\/p>\n<p>[java]<br \/>\nimport org.apache.http.entity.mime.MultipartEntity<br \/>\nimport org.apache.http.entity.mime.HttpMultipartMode<br \/>\nimport org.apache.http.entity.mime.content.InputStreamBody<br \/>\nimport org.apache.http.entity.mime.content.StringBody<br \/>\nimport groovyx.net.http.*<\/p>\n<p>void sendMultiPartFile(CommonsMultipartFile multipartImageFile, String cityName) {<\/p>\n<p> def http = new HTTPBuilder(&quot;http:\/\/www.anotherapp.com\/controller\/action&quot;)<\/p>\n<p> http.request(Method.POST) { req -&gt;<\/p>\n<p> requestContentType: &quot;multipart\/form-data&quot;<\/p>\n<p> MultipartEntity multiPartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)<\/p>\n<p> \/\/ Adding Multi-part file parameter &quot;imageFile&quot;<\/p>\n<p> multiPartContent.addPart(&quot;imageFile&quot;, new InputStreamBody(multipartImageFile.inputStream, multipartImageFile.contentType, multipartImageFile.originalFilename))<\/p>\n<p> \/\/ Adding another string parameter &quot;city&quot;<\/p>\n<p> multiPartContent.addPart(&quot;city&quot;, new StringBody(cityName))<\/p>\n<p> req.setEntity(multiPartContent)<\/p>\n<p> response.success = { resp -&gt;<\/p>\n<p>        if (resp.statusLine.statusCode == 200) {<\/p>\n<p>                  \/\/ response handling<\/p>\n<p>                   }<br \/>\n            }<br \/>\n      }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>Hope it would help!!!<br \/>\n~~<br \/>\nRegards<br \/>\nDivya Setia<br \/>\ndivya@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had two applications that communicate with each other through web-services. There I had a requirement to send multi-part file from one application to another. After searching I came out with the following solution and thought to share: [java] import org.apache.http.entity.mime.MultipartEntity import org.apache.http.entity.mime.HttpMultipartMode import org.apache.http.entity.mime.content.InputStreamBody import org.apache.http.entity.mime.content.StringBody import groovyx.net.http.* void sendMultiPartFile(CommonsMultipartFile multipartImageFile, String cityName) { [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":106},"categories":[7],"tags":[684,71,9,910,70,908,909],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6796"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6796"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6796\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}