We have a scenario, where the headers are different for different targeturl and hence want to condition before sending it. We are looking to set the request headers of service callout dynamically and unable to do the same.
context.setvariable('request.header
context.setvariable('servicecallout.request.header
Neither of it works unless the specification is directly done in service callout.
APIGEE version: 4.19.1
Can you provide your service callout snippet?
Hi @Karthiga Baskaran
If your different targeturl has same number of headers you can use following solution.
It's a two step process. In first step use Javascript to set up the values of headers dynamically based on your targeturl condition. Service call out will use those context variable values during call out.
// Function call to Set Headers for Service Callout Request setServiceCalloutHeaderValues(); function setServiceCalloutHeaderValues() { // Assume you are going to go on Target backened a var targetbacked = "a"; // set targetbackend to new value to b and see if service call out is picking up correct headers for new backend // var targetbacked = "b"; if (targetbacked === "a") { //Set values Context Variable employeeIdVal and employeedeptVal which will be passed during servicecall out step context.setVariable('employeeIdVal',"A.8795"); context.setVariable('employeedeptVal',"A.Accounts"); } else { context.setVariable('employeeIdVal',"B.8795"); context.setVariable('employeedeptVal',"B.HR"); } }
and your Service call out policy will look something like this.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ServiceCallout async="false" continueOnError="false" enabled="true" name="MyServiceCallOutTest"> <DisplayName>MyServiceCallOutTest</DisplayName> <Properties/> <Request clearPayload="true" variable="EmployeeServiceCalloutReq"> <Set> <Headers> <Header name="EmployeeId">{employeeIdVal}</Header> <Header name="EmployeeDepartment">{employeedeptVal}</Header> </Headers> <Verb>GET</Verb> </Set> </Request> <Response>EmployeeServiceCalloutResponse</Response> <HTTPTargetConnection> <Properties/> <URL>https://yourserviceurl.com</URL> </HTTPTargetConnection> </ServiceCallout>
in this example values for employeeIdVal and employeedeptVal is done in previous Javascript step. and your Service call out policy headers EmployeeId and EmployeeDepartment will use those values.
If you have different number of headers for target back end, you can set up different service call out policy for each target back end and invoke it conditionally.
alternatively you can use httpClient.send or httpClient.get using Javascript or Node.JS call outs to call target url where you can control thing dynamically.
Depending on what your requirement and design constrains, you can pick correct approach for the solution.
Thanks & Regards
Jayesh