Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send POST Request using Apache Camel

I was able to send a GET request using Apache Camel to a REST service and now I'm trying to send a POST request with a JSON body using Apache Camel. I wasn't able to figure out how to add the JSON body and send the request. How can I add a JSON body, send the request and get the response code?

like image 833
user6641655 Avatar asked Mar 02 '17 20:03

user6641655


3 Answers

Below you can find a sample Route which sends (every 2 seconds) the json, using POST method to the server, in the example it is localhost:8080/greeting. There is also a way to get the response presented:

from("timer://test?period=2000")
    .process(exchange -> exchange.getIn().setBody("{\"title\": \"The title\", \"content\": \"The content\"}"))
    .setHeader(Exchange.HTTP_METHOD, constant("POST"))
    .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
    .to("http://localhost:8080/greeting")
    .process(exchange -> log.info("The response code is: {}", exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE)));

Usually it is not a good idea to prepare json manually. You can use e.g.

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-gson</artifactId>
</dependency>

to perform marshalling for you. Assuming you have a Greeting class defined you can modify the Route by removing the first processor and using the following code instead:

.process(exchange -> exchange.getIn().setBody(new Greeting("The title2", "The content2")))
.marshal().json(JsonLibrary.Gson)

Further reading: http://camel.apache.org/http.html It is worth noting that there is also http4 component (they use different version of Apache HttpClient under the hood).

like image 54
kris_k Avatar answered Oct 30 '22 02:10

kris_k


This is how you can do it:

from("direct:start")
 .setHeader(Exchange.HTTP_METHOD, constant("POST"))
 .to("http://www.google.com");

Current Camel Exchange's body will get POSTED to the URL end point.

like image 33
Saurabh Nayar Avatar answered Oct 30 '22 04:10

Saurabh Nayar


//This code is for sending post request and getting response
public static void main(String[] args) throws Exception {
    CamelContext c=new DefaultCamelContext();       
    c.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {          
            from("direct:start")
            .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                System.out.println("i am worlds fastest flagship processor ");
                exchange.getIn().setHeader("CamelHttpMethod", "POST");
                exchange.getIn().setHeader("Content-Type", "application/json");
                exchange.getIn().setHeader("accept", "application/json");
                }
                })
              // to the http uri
                .to("https://www.google.com")
       // to the consumer
                .to("seda:end");
        }
    });



    c.start();
    ProducerTemplate pt = c.createProducerTemplate();
      // for sending request 
    pt.sendBody("direct:start","{\"userID\": \"678\",\"password\": \"password\", 
  \"ID\": \"123\"  }");
    ConsumerTemplate ct = c.createConsumerTemplate();
    String m = ct.receiveBody("seda:end",String.class);
    System.out.println(m);
}
like image 2
user12766534 Avatar answered Oct 30 '22 04:10

user12766534