You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by wheli <mi...@medfx.com> on 2016/07/22 14:56:01 UTC

HTTP POST with ProducerTemplate

Hello,

I am attempting to make an HTTP POST to an endpoint (using
"https://httpbin.org/post" as a test), but am having trouble figuring out
how to pull the HTTP response code (200, 404, etc...). I need to use a
ProducerTemplate due to some other restrictions in my application. I looked
the camel documentation and see some examples, but nothing that really
matches what I am trying to do.  

My current code:

public void sendExchangeToEndpoint(ProducerTemplate template, Exchange
exchange, String endpoint) {
String response = template.requestBody(endpoint,
exchange.getOut().getBody(), String.class);
.....
}

This works and returns what I post (which is how httpbin works), but I am
only getting the body back as a string. What would be the best way to get
back a full exchange that would contain body, headers, etc...

Any help would be greatly appreciated. Thanks! 



--
View this message in context: http://camel.465427.n5.nabble.com/HTTP-POST-with-ProducerTemplate-tp5785366.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: HTTP POST with ProducerTemplate

Posted by wheli <mi...@medfx.com>.
Ok. I was able to figure this out.

Changed my code to:

Exchange response = template.send(endpoint, new
HttpOutboundProcessor(exchange.getOut().getBody().toString()));
Integer responseCode =
(Integer)response.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE);
String responseBody =
IOUtils.toString((InputStream)response.getOut().getBody());

And that seems to work. I created a new, local, HttpOutboundProcessor class:

public class HttpOutboundProcessor implements Processor {

    private String exchangeBody;

    public HttpOutboundProcessor(String exchangeBody) {
        this.exchangeBody = exchangeBody;
    }

    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
        exchange.getIn().setBody(exchangeBody);
    }
}

Thank you for the help.


P.S. I tried replacing the whole exchange in the "process" method, but that
caused issues.



--
View this message in context: http://camel.465427.n5.nabble.com/HTTP-POST-with-ProducerTemplate-tp5785366p5785448.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: HTTP POST with ProducerTemplate

Posted by wheli <mi...@medfx.com>.
Ok...how or where would I set the body of the http post in that example? The
new Processor could not have context to anything outside its constructor,
right?



--
View this message in context: http://camel.465427.n5.nabble.com/HTTP-POST-with-ProducerTemplate-tp5785366p5785430.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: HTTP POST with ProducerTemplate

Posted by "Goyal, Arpit" <ar...@sap.com>.
Sorry I didn't read your question fully.

We are defining it this way:

Exchange exchange = template.send(URI, new Processor() {
   public void process(Exchange exchange) throws Exception {
	exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
  }
});
//Now you can get the out object and 
exchange.getOut().getHeader("CamelHttpResponseCode");

Hope this solves your problem.

Regards,
Arpit.

-----Original Message-----
From: Goyal, Arpit [mailto:arpit.goyal@sap.com] 
Sent: Friday, July 22, 2016 11:16 AM
To: users@camel.apache.org
Subject: RE: HTTP POST with ProducerTemplate

Hi Michael,

I was going through our implementation and looks like in the response Exchange --> Out object --> there is Camel Header which has the status. 

Wasn't able to attach screenshot of my debug variables, but here it goes:

DefaultExchange --> DefaultMessage (out object)  --> CaseInsensitiveMap (headers object) --> CamelHttpResponseCode (property).

Regards,
Arpit.

-----Original Message-----
From: wheli [mailto:michael.whelihan@medfx.com] 
Sent: Friday, July 22, 2016 7:56 AM
To: users@camel.apache.org
Subject: HTTP POST with ProducerTemplate

Hello,

I am attempting to make an HTTP POST to an endpoint (using
"https://httpbin.org/post" as a test), but am having trouble figuring out
how to pull the HTTP response code (200, 404, etc...). I need to use a
ProducerTemplate due to some other restrictions in my application. I looked
the camel documentation and see some examples, but nothing that really
matches what I am trying to do.  

My current code:

public void sendExchangeToEndpoint(ProducerTemplate template, Exchange
exchange, String endpoint) {
String response = template.requestBody(endpoint,
exchange.getOut().getBody(), String.class);
.....
}

This works and returns what I post (which is how httpbin works), but I am
only getting the body back as a string. What would be the best way to get
back a full exchange that would contain body, headers, etc...

Any help would be greatly appreciated. Thanks! 



--
View this message in context: http://camel.465427.n5.nabble.com/HTTP-POST-with-ProducerTemplate-tp5785366.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: HTTP POST with ProducerTemplate

Posted by "Goyal, Arpit" <ar...@sap.com>.
Hi Michael,

I was going through our implementation and looks like in the response Exchange --> Out object --> there is Camel Header which has the status. 

Wasn't able to attach screenshot of my debug variables, but here it goes:

DefaultExchange --> DefaultMessage (out object)  --> CaseInsensitiveMap (headers object) --> CamelHttpResponseCode (property).

Regards,
Arpit.

-----Original Message-----
From: wheli [mailto:michael.whelihan@medfx.com] 
Sent: Friday, July 22, 2016 7:56 AM
To: users@camel.apache.org
Subject: HTTP POST with ProducerTemplate

Hello,

I am attempting to make an HTTP POST to an endpoint (using
"https://httpbin.org/post" as a test), but am having trouble figuring out
how to pull the HTTP response code (200, 404, etc...). I need to use a
ProducerTemplate due to some other restrictions in my application. I looked
the camel documentation and see some examples, but nothing that really
matches what I am trying to do.  

My current code:

public void sendExchangeToEndpoint(ProducerTemplate template, Exchange
exchange, String endpoint) {
String response = template.requestBody(endpoint,
exchange.getOut().getBody(), String.class);
.....
}

This works and returns what I post (which is how httpbin works), but I am
only getting the body back as a string. What would be the best way to get
back a full exchange that would contain body, headers, etc...

Any help would be greatly appreciated. Thanks! 



--
View this message in context: http://camel.465427.n5.nabble.com/HTTP-POST-with-ProducerTemplate-tp5785366.html
Sent from the Camel - Users mailing list archive at Nabble.com.