This example demonstrates how to use KrakenD to implement multiple POST calls in a sequential proxy, dynamically transform data from a GET request into a POST call, inject data from one service's response into another POST request, and aggregate the responses.
Note |
---|
Using sequential calls is considered an anti-pattern because when you make a network service dependent on the other, you are increasing the latency, decreasing the performance, and eventually augmenting the error rate. Read more here » |
We have two services:
- User Service: Returns user data based on a
username
. - Reviews Service: Returns all reviews from a user based on their
user_id
.
We'll implement a single GET endpoint in KrakenD that:
- Extracts the
username
from the GET request path. - Uses this
username
to make a POST request to the User Service. - Takes the
user_id
from the User Service response and uses it to make a POST request to the Reviews Service. - Aggregates the responses from both services into a single response.
- KrakenD Enterprise license (required for dynamic routing and request modifiers with templates).
- Docker and Docker Compose installed.
docker-compose.yml
: Docker Compose file including KrakenD Enterprise and basic services to simulate the User and Reviews services.index.php
: Basic implementation of the User and Reviews services.krakend.json
: KrakenD configuration file.LICENSE
: Placeholder for a valid KrakenD Enterprise license.reviews_post_body.tmpl
: Template for the Reviews Service POST request body.users_post_body.tmpl
: Template for the User Service POST request body.
-
Start the Services:
docker-compose up
-
Make a GET Request:
curl http://localhost:8080/user-and-reviews/admin
- The GET request to
/user-and-reviews/{username}
will extract theusername
from the path. - KrakenD will use this
username
to make a POST request to the User Service. - The
user_id
from the User Service response will be extracted and used to make a POST request to the Reviews Service. - The responses from both services will be aggregated and returned in a single response.
- Ensure you replace the
LICENSE
file with a valid KrakenD Enterprise license to run this example. - Adjust the paths and configuration in
krakend.json
as needed to match your setup.
This example showcases KrakenD's powerful capabilities to handle complex API orchestration scenarios with ease. For more details, visit krakend.io.