How can I copy the target response into a request message for use in a service callout policy?

Not applicable

I need to use the target endpoint response payload as the request payload in a subsequent service callout policy with apigee to be able to log the response using a 3rd party service.

I have tried using:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Create-New-Message">
    <DisplayName>Create New Message</DisplayName>
    <AssignTo createNew="true" type="request">newrequest</AssignTo>
    <Copy source='response'>
      <Payload>{response.content}</Payload>
    </Copy>
    <Set>
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage> 

But the payload remains empty.

Solved Solved
0 5 3,380
2 ACCEPTED SOLUTIONS

Mark, I've misunderstood. I'm editing my answer.

Rather than using the Copy element in Assign Message, I would suggest that you use the Set element to set the Payload. like this:

<AssignMessage name="Create-New-Message">
  <DisplayName>Create New Message</DisplayName>
  <AssignTo createNew="true" type="request">newrequest</AssignTo>
  <Set>
    <Payload>{response.content}</Payload>
    <Verb>POST</Verb>
  </Set>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage> 

This will work only in a response flow, obviously. The response message will have been set only there.

You may also want to set request headers like Content-Type, Accept, Authorization, and so on. You can do all of that within the same AssignMessage.

View solution in original post

Hi Mark,

I have tested out Dino's policy, and it works for me when placed somewhere in the response flow (where the response exists). My recommendation would be to add a JavaScript policy before and after your AssignMessage policy to check the value of variables:

context.getVariable("response.content");
context.getVariable("newrequest.content");
context.getVariable("newrequest.verb");

When you trace the code, you'll see these values in the trace when selecting the JavaScript policies. In my case, after Dino's policy, response.content and newrequest.content match. I didn't need to add a contentType attribute.

My best guess would be that response.content doesn't contain what you think it contains at the time you do the AssignMessage.

I'd also just try setting IgnoreUnresolvedVariables to true in that policy just to make sure there isn't something weird going on there. Another thing to check is to look at the entire proxy/target endpoint code file (click on the name of the proxy or target that you have the policy in) and make sure the policy is attached exactly where you expect.

Mike

View solution in original post

5 REPLIES 5

Mark, I've misunderstood. I'm editing my answer.

Rather than using the Copy element in Assign Message, I would suggest that you use the Set element to set the Payload. like this:

<AssignMessage name="Create-New-Message">
  <DisplayName>Create New Message</DisplayName>
  <AssignTo createNew="true" type="request">newrequest</AssignTo>
  <Set>
    <Payload>{response.content}</Payload>
    <Verb>POST</Verb>
  </Set>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage> 

This will work only in a response flow, obviously. The response message will have been set only there.

You may also want to set request headers like Content-Type, Accept, Authorization, and so on. You can do all of that within the same AssignMessage.

Not applicable

Hi,

Still doesn't set the payload. Correctly sets the verb and I can add query parameters but my payload remains empty. Also fails to set the payload if I use something like:

<Set>
    <QueryParams>
	<QueryParam name="address">1234</QueryParam>
    </QueryParams>
    <Verb>POST</Verb>
    <Payload>1234</Payload>
</Set>
<br>

Mark.

Mark, can you try with the contentType attribute, like this?

  <Set>
    <Payload contentType='text/plain'>1234</Payload>
  </Set>

Other valid values for contentType are: application/xml, application/json, and so on.

You should use a value that is appropriate for your payload.

Hi Mark,

I have tested out Dino's policy, and it works for me when placed somewhere in the response flow (where the response exists). My recommendation would be to add a JavaScript policy before and after your AssignMessage policy to check the value of variables:

context.getVariable("response.content");
context.getVariable("newrequest.content");
context.getVariable("newrequest.verb");

When you trace the code, you'll see these values in the trace when selecting the JavaScript policies. In my case, after Dino's policy, response.content and newrequest.content match. I didn't need to add a contentType attribute.

My best guess would be that response.content doesn't contain what you think it contains at the time you do the AssignMessage.

I'd also just try setting IgnoreUnresolvedVariables to true in that policy just to make sure there isn't something weird going on there. Another thing to check is to look at the entire proxy/target endpoint code file (click on the name of the proxy or target that you have the policy in) and make sure the policy is attached exactly where you expect.

Mike

Hi Mark,

Just realized that your Copy in your original question isn't exactly right. You should use the following to copy the response payload to the new message's payload:

  <Copy source="response">
    <Payload/>
  </Copy>