Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Adding a Custom Header to Service callout using Assign Message or Java Script

We have a requirement to call a target via service callout policy. Where the name of the header in the policy should be dynamically set during deployment(can be taken care in the CICD pipeline).We tried using a Java script and assign Message policy both of which throws a class cast exception.

Could you please help in figuring out what the issue could be. Or suggest any alternate approach if any.

Error:

10211-one.png

10212-two.png

10213-three.png

below is my service callout policy.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="SC-TargetendpointCall3">
    <DisplayName>SC-TargetendpointCall3</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest3">
        <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
        <Set>
            <Headers>
                <Header name="Content-Type">{ContentType}</Header>
            </Headers>
            <Verb>{request.verb}</Verb>
            <Path>{sc_urlPath}</Path>
        </Set>
        <Copy source="request">
            <QueryParams/>
        </Copy>
    </Request>
    <Response>calloutResponseCall3</Response>
    <HTTPTargetConnection>
        <URL>http://This is a dummy endpoint</URL>
    </HTTPTargetConnection>
</ServiceCallout>
Solved Solved
0 6 1,914
1 ACCEPTED SOLUTION

Vidhya - Can you show the AssignMessage or JS in which you set myRequest2?

In a ServiceCallout, if you specify something like this:

<Request clearPayload="true" variable="myRequest2">
...

...the policy assumes you have previously set a variable called myRequest2 with a message value.

If you have a prior AssignMessage or JS that simply sets "myRequest2.header.abc", that is not enough. That doesn't create a message object.

In other words, This Will Not Satisfy:

<AssignMessage name='AM-MyMessage'>
  <AssignVariable>
    <Name>myRequest2.header.abc</Name>
    <Value>whatever-i-like</Value>
  </AssignVariable>
</AssignMessage>

And also, this Will Not Satisfy in JS:

context.setVariable('myRequest2.header.abc', 'whatever-i-like');

In both cases, the policy will "work" in that it will succeed with no errors. But the side effect will have been to set a string variable, not to set a message variable.

Instead you need to create a full request message. Like this with AssignMessage:

<AssignMessage name='AM-MyMessage'>
  <AssignTo createNew='true' transport='http' type='request'>myRequest2</AssignTo>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <Set>
    <Headers>    
      <Header name='abc'>whatever-you-like</Header>
    </Headers>
  </Set>
</AssignMessage>

OR, like this in JS:

  var url = properties.target + '/oneid/' + id ;
  var headers = { abc : 'whatever-you-like' };
  var req = new Request(url, 'GET', headers);
  context.setVariable('myRequest2', req);<br>

View solution in original post

6 REPLIES 6