Dinamically replacing target server or host before request

Not applicable

I have a requirement for pointing to a mock server (in this case wiremock) that will serve responses for my requests instead of going to the real back end services. This will allow us to keep working on the apigee implementations as long as we know the expected responses for some back end request. I went back and forth through this site looking for similar solutions but haven´t found any yet. They are going to pass a query param like mock=true and when it's detected i need to just change the host in my final request. I checked for example accessing target.host or target.url variables but they are not available when using target servers for example. Any generic solution I could apply here? Any help or guidance would be very appreciated.

0 1 275
1 REPLY 1

Hi @Raul Nunez, you can use below code in Target Endpoint preflow to achieve this; This will read a target server defined in edge environment configuration under Target Servers;

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
    <Description/>
    <FaultRules/>
    <PreFlow name="PreFlow">
        <Request/>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows/>
    <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="my-target-server"/>
        </LoadBalancer>
    </HTTPTargetConnection>
</TargetEndpoint>

Make sure you define a Target Server first, initially you can use the value as mock as per your requirement.

You can read more about this feature here - https://docs.apigee.com/api-platform/deploy/load-balancing-across-backend-servers