One Star

tRestClient MIME Multipart Form Data POST to post zip file

Hi
How can I use tRestClient to post a zip file to a webservice?
This should be doen as  MIME Multipart Form Data POST request.
Any help is appreciated.
tx
Luc
4 REPLIES
One Star

Re: tRestClient MIME Multipart Form Data POST to post zip file

Hi
I noticed that effort is been put in place to extend the tRest(serverside)components to support MIME Multipart Form.
How could we have the same on the consumer side for tRestClient?
We are using tRestClient to consume succefully a Rest webservice (including oAuth) via Get and Post calls with XML Content type.
Now we need to be able to post a zip file.
tx
Luc
One Star

Re: tRestClient MIME Multipart Form Data POST to post zip file

I have the same problem with a image file
One Star

Re: tRestClient MIME Multipart Form Data POST to post zip file

Hi
This is how we solved it in a quick and dirty hacky way, but it works.

Under TOS_ESB-r117442-V5.4.2\Studio\plugins\org.talend.designer.esb.components.rs.consumer_5.4.2.r117442\components\tRESTClient

(make a copy of the directory in case it does not work)
-------------------------------------------------------------

in tRESTClient_main.javajet after line 315 

I added this:
<% } else if ("multipart/form-data".equals(contentType) && !"GET".equals(method) && !"DELETE".equals(method)) { %>   
        <% for (Map<String, String> query : queries) { %>
            System.out.println("file=" + <%=query.get("VALUE")%>);
            String zipfilename = <%=query.get("VALUE")%>.contains("/")?<%=query.get("VALUE")%>.substring(<%=query.get("VALUE")%>.lastIndexOf("/")+1):<%=query.get("VALUE")%>;
            org.apache.cxf.jaxrs.ext.multipart.ContentDisposition cd_<%=cid%> = new org.apache.cxf.jaxrs.ext.multipart.ContentDisposition("attachment;filename=" + zipfilename);
            java.io.InputStream is_<%=cid%> = new java.io.FileInputStream(<%=query.get("VALUE")%>);
            org.apache.cxf.jaxrs.ext.multipart.Attachment att_<%=cid%> = new org.apache.cxf.jaxrs.ext.multipart.Attachment("root", is_<%=cid%> , cd_<%=cid%>);
            requestBody_<%=cid%> = new org.apache.cxf.jaxrs.ext.multipart.MultipartBody(att_<%=cid%>);
        <% } %>

NOTE: you can see (in the new version at line 320 at the end)  this is a bit hardcoded here in the sense that the queryparameter is hardcoded and named "zipfile".
-------------------------------------------------------------
in  tRESTClient_java.xml after line 80
I added this:
<ITEM NAME="MULTIPARTFORM" VALUE="multipart/form-data" />
-------------------------------------------------------------
forgot to mention:
in file tRESTClient_messages.properties
After line 35:
added:
CONTENT_TYPE.ITEM.MULTIPARTFORM=MULTIPARTFORM


------------------------------------------------------------
restart Talend and drag the component on the canvas.
------------------------------------------------------------
In the user infterface (component)

With the Content-type dropdwon now contains "MULTIPARTFORM" (select this)
In the query parameters section add a parameter (clicking the + button) and name the parameter "zipfile", (see the note higher up on the hardcoding)
while the value for this query parameter named "zipfile"  can be set like any other values in Talend. The value should point to a location to get the zipfile (or in your case the imagefile).
Hoping this will help. I cannot take any responsibility in this.
I hope it might be picked up by propper Talend developers ;-)
Six Stars

Re: tRestClient MIME Multipart Form Data POST to post zip file

Hi Luc,
The line numbers are not matching the current Talend version anymore. I had the same problem but a slightly different solution which I documented overhere
It would be better if Talend includes this functionality in the component in a future release, but for now this works for me.
-----------------------------------------------------------------------------------------------------------
When you like my answer, please accept it as solution and send some kudos