You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Claus Ibsen <cl...@gmail.com> on 2014/06/13 08:15:59 UTC

Re: Camel 2.12.1 : How to send Http post with an attachment using ProducerTemplate

Hi

A good idea is to check the unit tests of the components. Maybe you
can find some sample code there.

For http try look in
https://github.com/apache/camel/tree/master/components/camel-http/src/test/java/org/apache/camel/component/http

and
https://github.com/apache/camel/tree/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty

On Thu, Jun 12, 2014 at 9:07 PM, Shing Hing Man
<ma...@yahoo.com.invalid> wrote:
> Hi,
>
>  I tried to use producerTemplate to send a http post with an attached file.
>
>
>
> ProducerTemplate template = context.createProducerTemplate();
>
>         Exchange exchange = template.send("http://localhost:8080/file",
>                 new Processor() {
>                     public void process(Exchange exchange) throws Exception {
>
>                         Message msgIn = exchange.getIn();
>                         String userHome=System.getProperty("user.home");
>                         File file = new File(userHome + "/test.txt");
>                         DataHandler dh = new DataHandler(new FileDataSource(file));
>
>                         msgIn.addAttachment("myFile",dh);
>                         msgIn.setHeader(Exchange.CONTENT_TYPE, "multipart/form-data");
>
>                         msgIn.setHeader(Exchange.HTTP_METHOD, "POST");
>                     }
>
>         });
>
> At the server side, I received something like :
>
>
> RequestContext(HttpRequest(POST,http://localhost:8080/file,List(Content-Length: 0, Host: localhost:8080, User-Agent: Jakarta Commons-HttpClient/3.1, breadcrumbId: ID-gauss-site-41171-1402599541172-0-1),Empty,HTTP/1.1),Actor[akka://default/temp/$a],)
>
> The Context type and the attached file seem not to have reached the server.
>
>
> Is the above a correct way  to send http post with attachment ?
>
> Thanks in advance for any assistance !
>
> Shing



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Re: Camel 2.12.1 : How to send Http post with an attachment using ProducerTemplate

Posted by Shing Hing Man <ma...@yahoo.com.INVALID>.
I have tried your suggestion and it works.  

Thanks!

Shing



On Monday, 16 June 2014, 3:10, Willem Jiang <wi...@gmail.com> wrote:
 


camel-http component support to send RequestEntity by default.
If you want to send a file as a multi part form, you can just put the MultipartRequestEntity instance into the message body, just like this.

private RequestEntity createMultipartRequestEntity() throws Exception {
        File file = new File("src/main/resources/META-INF/NOTICE.txt");

        Part[] parts = {new StringPart("comment", "A binary file of some kind"),
                        new FilePart(file.getName(), file)};

        return new MultipartRequestEntity(parts, new HttpMethodParams());

    } 
    
    @Test
    public void
 testSendMultiPartFormFromCamelHttpComponnent() throws Exception {
        String result = template.requestBody("http://localhost:" + getPort() + "/test", createMultipartRequestEntity(), String.class);
        assertEquals("Get a wrong result", "A binary file of some kind", result);
    }

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem



On June 15, 2014 at 5:16:22 PM, Shing Hing Man (matmsh@yahoo.com.invalid) wrote:
> Thanks for the link !
>  
>  
> I found an example in
>  
>  
> https://github.com/apache/camel/blob/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpSendFileTest.java  
>  
>  
> I have noticed from the example
>  
>  
> https://github.com/apache/camel/blob/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java  
>  
> that, Apache HttpClient is used to send a MultiPartForm.
>  
> It looks as though one can not send a MultiPartForm (with more than one body parts) using  
> Camel ProducerTemplate.
>  
>  
>  
> Shing
>  
>  
>  
>  
> On Friday, 13 June 2014, 7:16, Claus Ibsen wrote:

>  
>  
>  
> Hi
>  
> A good idea is to check the unit tests of the components. Maybe you
> can find some sample code there.
>  
> For http try look in
> https://github.com/apache/camel/tree/master/components/camel-http/src/test/java/org/apache/camel/component/http  
>  
> and
> https://github.com/apache/camel/tree/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty  
>  
>  
> On Thu, Jun 12, 2014 at 9:07 PM, Shing Hing Man
> wrote:
> > Hi,
> >
> > I tried to use producerTemplate to send a http post with an attached file.
> >
> >
> >
> > ProducerTemplate template = context.createProducerTemplate();
> >
> > Exchange exchange = template.send("http://localhost:8080/file",
> > new Processor() {
> > public void process(Exchange exchange) throws Exception {
> >
> > Message msgIn = exchange.getIn();
> > String userHome=System.getProperty("user.home");
> > File file = new File(userHome + "/test.txt");
> > DataHandler dh = new DataHandler(new FileDataSource(file));
> >
> > msgIn.addAttachment("myFile",dh);
> > msgIn.setHeader(Exchange.CONTENT_TYPE, "multipart/form-data");
> >
> > msgIn.setHeader(Exchange.HTTP_METHOD, "POST");
> > }
> >
> > });
> >
> > At the server side, I received something like :
> >
> >
> > RequestContext(HttpRequest(POST,http://localhost:8080/file,List(Content-Length:  
> 0, Host: localhost:8080, User-Agent: Jakarta Commons-HttpClient/3.1, breadcrumbId:  
> ID-gauss-site-41171-1402599541172-0-1),Empty,HTTP/1.1),Actor[akka://default/temp/$a],)  
> >
> > The Context type and the attached file seem not to have reached the server.
> >
> >
> > Is the above a correct way to send http post with attachment ?
> >
> > Thanks in advance for any assistance !
> >
> > Shing
>  
>  
>  
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> Email: cibsen@redhat.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
> hawtio: http://hawt.io/
> fabric8: http://fabric8.io/

Re: Camel 2.12.1 : How to send Http post with an attachment using ProducerTemplate

Posted by Willem Jiang <wi...@gmail.com>.
camel-http component support to send RequestEntity by default.
If you want to send a file as a multi part form, you can just put the MultipartRequestEntity instance into the message body, just like this.

private RequestEntity createMultipartRequestEntity() throws Exception {
        File file = new File("src/main/resources/META-INF/NOTICE.txt");

        Part[] parts = {new StringPart("comment", "A binary file of some kind"),
                        new FilePart(file.getName(), file)};

        return new MultipartRequestEntity(parts, new HttpMethodParams());

    } 
    
    @Test
    public void testSendMultiPartFormFromCamelHttpComponnent() throws Exception {
        String result = template.requestBody("http://localhost:" + getPort() + "/test", createMultipartRequestEntity(), String.class);
        assertEquals("Get a wrong result", "A binary file of some kind", result);
    }

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem



On June 15, 2014 at 5:16:22 PM, Shing Hing Man (matmsh@yahoo.com.invalid) wrote:
> Thanks for the link !
>  
>  
> I found an example in
>  
>  
> https://github.com/apache/camel/blob/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpSendFileTest.java  
>  
>  
> I have noticed from the example
>  
>  
> https://github.com/apache/camel/blob/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java  
>  
> that, Apache HttpClient is used to send a MultiPartForm.
>  
> It looks as though one can not send a MultiPartForm (with more than one body parts) using  
> Camel ProducerTemplate.
>  
>  
>  
> Shing
>  
>  
>  
>  
> On Friday, 13 June 2014, 7:16, Claus Ibsen wrote:
>  
>  
>  
> Hi
>  
> A good idea is to check the unit tests of the components. Maybe you
> can find some sample code there.
>  
> For http try look in
> https://github.com/apache/camel/tree/master/components/camel-http/src/test/java/org/apache/camel/component/http  
>  
> and
> https://github.com/apache/camel/tree/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty  
>  
>  
> On Thu, Jun 12, 2014 at 9:07 PM, Shing Hing Man
> wrote:
> > Hi,
> >
> > I tried to use producerTemplate to send a http post with an attached file.
> >
> >
> >
> > ProducerTemplate template = context.createProducerTemplate();
> >
> > Exchange exchange = template.send("http://localhost:8080/file",
> > new Processor() {
> > public void process(Exchange exchange) throws Exception {
> >
> > Message msgIn = exchange.getIn();
> > String userHome=System.getProperty("user.home");
> > File file = new File(userHome + "/test.txt");
> > DataHandler dh = new DataHandler(new FileDataSource(file));
> >
> > msgIn.addAttachment("myFile",dh);
> > msgIn.setHeader(Exchange.CONTENT_TYPE, "multipart/form-data");
> >
> > msgIn.setHeader(Exchange.HTTP_METHOD, "POST");
> > }
> >
> > });
> >
> > At the server side, I received something like :
> >
> >
> > RequestContext(HttpRequest(POST,http://localhost:8080/file,List(Content-Length:  
> 0, Host: localhost:8080, User-Agent: Jakarta Commons-HttpClient/3.1, breadcrumbId:  
> ID-gauss-site-41171-1402599541172-0-1),Empty,HTTP/1.1),Actor[akka://default/temp/$a],)  
> >
> > The Context type and the attached file seem not to have reached the server.
> >
> >
> > Is the above a correct way to send http post with attachment ?
> >
> > Thanks in advance for any assistance !
> >
> > Shing
>  
>  
>  
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> Email: cibsen@redhat.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
> hawtio: http://hawt.io/
> fabric8: http://fabric8.io/


Re: Camel 2.12.1 : How to send Http post with an attachment using ProducerTemplate

Posted by Shing Hing Man <ma...@yahoo.com.INVALID>.
Thanks for the link !


I found an example in 


https://github.com/apache/camel/blob/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpSendFileTest.java


I have noticed from the example 


https://github.com/apache/camel/blob/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java

that, Apache  HttpClient is used to send a MultiPartForm.

It looks as though one can not send a MultiPartForm (with more than one body parts)  using Camel ProducerTemplate. 



Shing




On Friday, 13 June 2014, 7:16, Claus Ibsen <cl...@gmail.com> wrote:
 


Hi

A good idea is to check the unit tests of the components. Maybe you
can find some sample code there.

For http try look in
https://github.com/apache/camel/tree/master/components/camel-http/src/test/java/org/apache/camel/component/http

and
https://github.com/apache/camel/tree/master/components/camel-jetty/src/test/java/org/apache/camel/component/jetty


On Thu, Jun 12, 2014 at 9:07 PM, Shing Hing Man
<ma...@yahoo.com.invalid> wrote:
> Hi,
>
>  I tried to use producerTemplate to send a http post with an attached file.
>
>
>
> ProducerTemplate template = context.createProducerTemplate();
>
>         Exchange exchange = template.send("http://localhost:8080/file",
>                 new Processor() {
>                     public void process(Exchange exchange) throws Exception {
>
>                         Message msgIn = exchange.getIn();
>                         String userHome=System.getProperty("user.home");
>                         File file = new File(userHome + "/test.txt");
>                         DataHandler dh = new DataHandler(new FileDataSource(file));
>
>                         msgIn.addAttachment("myFile",dh);
>                         msgIn.setHeader(Exchange.CONTENT_TYPE, "multipart/form-data");
>
>                         msgIn.setHeader(Exchange.HTTP_METHOD, "POST");
>                     }
>
>         });
>
> At the server side, I received something like :
>
>
> RequestContext(HttpRequest(POST,http://localhost:8080/file,List(Content-Length: 0, Host: localhost:8080, User-Agent: Jakarta Commons-HttpClient/3.1, breadcrumbId: ID-gauss-site-41171-1402599541172-0-1),Empty,HTTP/1.1),Actor[akka://default/temp/$a],)
>
> The Context type and the attached file seem not to have reached the server.
>
>
> Is the above a correct way  to send http post with attachment ?
>
> Thanks in advance for any assistance !
>
> Shing



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/