Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3522

How to Send a Post Request in Camel

$
0
0

1. Overview

Apache Camel is a robust open-source integration framework. It provides a mature set of components to interact with various protocols and systems, including HTTP.

In this tutorial, we’ll explore the Apache Camel HTTP component and demonstrate how to initiate a POST request to JSONPlaceholder, a free fake API for testing and prototyping.

2. Apache Camel HTTP Component

The Apache Camel HTTP component provides functionality to communicate with an external web server. It supports various HTTP methods including GET, POST, PUT, DELETE, etc.

By default, the HTTP component uses port 80 for HTTP and port 443 for HTTPS. Here’s the general syntax for the HTTP component URI:

http://hostname[:port][/resourceUri][?options]

The component must start with the ‘http‘ or ‘https‘ scheme, followed by the hostname, optional port, resource path, and query parameters.

We can set the HTTP method using the httpMethod option in the URI:

https://jsonplaceholder.typicode.com/posts?httpMethod=POST

Also, we can set the HTTP method in the message header:

setHeader(Exchange.HTTP_METHOD, constant("POST"))

Setting the HTTP method is essential to initiate a request successfully.

3. Project Setup

To begin, let’s add the camel-core and camel-test-jnit5 dependencies to the pom.xml:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>4.6.0</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-test-junit5</artifactId>
    <version>4.6.0</version>
</dependency>

The camel-core dependency provides the core classes for system integration. One of the important classes is the RouteBuilder to create routes. The camel-test-junit5 provides support for testing Camel routes with JUnit 5.

Next, let’s add the camel-jackson and camel-http dependencies to the pom.xml:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jackson</artifactId>
    <version>4.6.0</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-http</artifactId>
    <version>4.6.0</version>
</dependency>

The camel-http dependency provides support for HTTP components to communicate with external servers. Also, we added camel-jackson dependency for JSON serialization and deserialization using Jackson.

Then, let’s create a sample JSON payload for the POST request to “https://jsonplaceholder.typicode.com/post“:

{
  "userId": 1,
  "title": "Java 21",
  "body": "Virtual Thread",
}

Here, the payload contains the userId, title, and body. We expect the endpoint to return HTTP status code 201 on the successful creation of a new post.

4. Sending Post Request

To begin, let’s create a class named PostRequestRoute which extends the RouteBuilder class:

public class PostRequestRoute extends RouteBuilder { 
}

The RouteBuilder class allows us to override the configure() method to create a route.

4.1. Sending Post Request With JSON String

Let’s define a route that sends a POST request to our dummy server:

from("direct:post").process(exchange -> exchange.getIn()
  .setBody("{\"title\":\"Java 21\",\"body\":\"Virtual Thread\",\"userId\":\"1\"}"))
  .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
  .to("https://jsonplaceholder.typicode.com/posts?httpMethod=POST")
  .to("mock:post");

Here, we define a route and set the payload as  JSON String. The setBody() body method accepts the JSON string as an argument. Also, we set the HTTP method to POST by using the httpMethod option.

Then, we send the request to the JSONPlacehoder API. Finally, we forward the response to a mock endpoint.

4.2. Sending Post Request With POJO Class

However, defining a JSON string could be error-prone. For a more type-safe approach, let’s define a POJO class named Post:

public class Post {
    private int userId;
    private String title;
    private String body;
    // standard constructor, getters, setters
}

Next, let’s modify our route to use the POJO class:

from("direct:start").process(exchange -> exchange.getIn()
  .setBody(new Post(1, "Java 21", "Virtual Thread"))).marshal().json(JsonLibrary.Jackson)
  .setHeader(Exchange.HTTP_METHOD, constant("POST"))
  .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
  .to("https://jsonplaceholder.typicode.com/posts")
  .process(exchange -> log.info("The HTTP response code is: {}", exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE)))
  .process(exchange -> log.info("The response body is: {}", exchange.getIn().getBody(String.class)))
  .to("mock:result");

Here, we start from a direct endpoint named start. Then, we create a Post instance and set it as the request body. Also, we marshal the POJO to JSON using Jackson.

Next, we send the request to the fake API and log the response code and body. Finally, we forward the response to a mock endpoint for testing purposes.

5. Testing the Route

Let’s write a test to verify our route behavior. First, let’s create a test class that extends the CamelTestSupport class:

class PostRequestRouteUnitTest extends CamelTestSupport {
}

Then, let’s create a mock endpoint and producer template:

@EndpointInject("mock:result")
protected MockEndpoint resultEndpoint;
@Produce("direct:start")
protected ProducerTemplate template;

Next, let’s override the createRouteBuilder() method to use PostRequesteRoute:

@Override
protected RouteBuilder createRouteBuilder() {
    return new PostRequestRoute();
}

Finally, let’s write a test method:

@Test
void whenMakingAPostRequestToDummyServer_thenAscertainTheMockEndpointReceiveOneMessage() throws Exception {
    resultEndpoint.expectedMessageCount(1);
    resultEndpoint.message(0).header(Exchange.HTTP_RESPONSE_CODE)
      .isEqualTo(201);
    resultEndpoint.message(0).body()
      .isNotNull();
    template.sendBody(new Post(1, "Java 21", "Virtual Thread"));
    resultEndpoint.assertIsSatisfied();
}

In the code above, we define expectations for the mock endpoint and send a request using the template.sendBody() method. Finally, we ascertain that the expectations set for the mock endpoint are met.

6. Conclusion

In this article, we learn how to make a POST request to an external server using Apache Camel. We start by defining a route for sending POST requests using both JSON string and POJO.

Also, we saw how to use the HTTP component to communicate with an external API. Finally, we wrote a unit test to verify our route behavior.

As usual, the complete source code for the examples is available over on GitHub.

       

Viewing all articles
Browse latest Browse all 3522

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>