Assign Message - AssignVariable JSON content

VAP
Bronze 4
Bronze 4

Hi,

I want to save the request body sent to the target server, for latter log. For that I used a AM policy in the postflow of the target 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-SetVarsToTarget">
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <DisplayName>AM-SetVarsToTarget</DisplayName>
    <AssignVariable>
        <Name>target.request.body</Name>
         <Value>DefaultReq</Value>
        <Ref>message.content</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>target.request.querystring</Name>
        <Ref>message.querystring</Ref>
    </AssignVariable>
</AssignMessage>

But when I try to use the target.request.body variable in the PostClientFlow, for logging purposes the variable is empty. When using the debug view I see a unequal sign next to the variable

VAP_0-1651865328103.png

Any clues of what I'm doing wrong here?

Thanks!

Solved Solved
0 3 829
1 ACCEPTED SOLUTION

YES

The ''unequal" sign means you cannot assign to that variable. (More precisely, it means the assign failed). I believe you want target.request.content.

Can you try assigning to that?

View solution in original post

3 REPLIES 3

YES

The ''unequal" sign means you cannot assign to that variable. (More precisely, it means the assign failed). I believe you want target.request.content.

Can you try assigning to that?

Hi @dchiesa1 , yes that worked. I guess its better name my own variables with names that don't collide with internal apigee vars (e.g. instead of target.request.content use totarget.request.content). Thanks for that.

I now face a different issue related with cloud logging API. The SC on the PostClientFlow is meant to call the GCloud logging API

 

<ServiceCallout name="SC.CloudLogging">
    <Request>
        <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
        <Set>
            <Payload contentType="application/json">{
    "logName": "projects/{organization.name}/logs/apigee-runtime",
    "resource" : {
      "type": "api",
      "labels": {}
    },
    "labels": {
        "application": "apigee",
        "proxy":"logging-example"
    },
    "entries": [{
        "severity": "INFO",
        "jsonPayload": {
            "organization": "{organization.name}",
            "environment": "{environment.name}",
            "apiProxy": "{apiproxy.name}",
            "apiProxyRevision": "{apiproxy.revision}",
            "apiProduct": "{apiproduct.name}",
            "developerApp": "{apiproduct.name}",
            "clientId": "{client_id}",
            "developerId": "{developer.id}",
            "requestUri": "{request.uri}",
            "requestUrl": "{request.url}",
            "verb": "{request.verb}",
            "correlationId": "{messageid}",
            "proxyRequestReceived": "{client.received.end.timestamp}",
            "proxyResponseSent": "{client.sent.end.timestamp}",
            "targetResponseReceived": "{target.received.end.timestamp}",
            "targetRequestSent": "{target.sent.end.timestamp}",
            "targetResponseCode": "{message.status.code}",
            "proxyResponseCode": "{response.status.code}",
            "clientReceived": "{client.received.start.timestamp}",
            "clientSent": "{client.sent.start.timestamp}",
            "targetUrl":"{target.url}",
            "target.request.body":"{totarget.request.content}"
            "target.request.querystring":"{totarget.request.querystring}",
            <!--"target.response.body":"{fromtarget.response.content}",-->
            "target.response.status.code":"{fromtarget.response.status.code}",
           <!-- "error.content":"{error.content}",-->
            "error.message":"{error.message}",
            "error.status.code":"{error.status.code}",
            "faultName":"{fault.name}"
        }
    }],
    "partialSuccess": true
  }
            </Payload>
            <Verb>POST</Verb>
        </Set>
    </Request>

    <HTTPTargetConnection>
        <Authentication>
            <GoogleAccessToken>
                <Scopes>
                    <Scope>https://www.googleapis.com/auth/logging.write</Scope>
                </Scopes>
                <LifetimeInSeconds>3600</LifetimeInSeconds>
            </GoogleAccessToken>
        </Authentication>
        <URL>https://logging.googleapis.com/v2/entries:write</URL>
    </HTTPTargetConnection>
</ServiceCallout>

 

The issue is  that every time I try to log the totarget.request.content var to GCloud API, the SC ends with an error. I imagine that's because I will have JSON inside JSON... If I disable that line the SC ends successfully.  Any ideas of how to get this log working? 

 Thanks!

Any ideas of how to get this log working?

yes. If you're certain that the fromtarget.response.content variable always holds JSON, then you can just eliminate the quotes. Like this:

 

"target.response.body": {fromtarget.response.content}, <!-- no quotes around this var ref -->

 

If that variable does not always hold correct json, then you can escape the content with escapeJSON :

 

"target.response.body": "{escapeJSON(fromtarget.response.content)}", <!-- escaped -->

 

In the latter case, if it IS JSON, then ... it will be embedded as a plain string.