I want to retrieve a value from kvm assign to a variable and print it out

I am using no target server.I have a kvm as my_name name

The code is as follows

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="Key-Value-Map-Operations-1" mapIdentifier="">
    <DisplayName>Key Value Map Operations-1</DisplayName>
    <Properties />
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
    <Get assignTo="movie.director">
        <Key>
            <Parameter ref="my_name" />
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

I want to print the print the variable with a assign message policy as

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties />
    <Copy source="request">
        <Payload>"movie.director"</Payload>
    </Copy>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request" />
</AssignMessage>

I get error

{
  "fault": {
    "faultstring": "java.lang.UnsupportedOperationException",
    "detail": {
      "errorcode": "Internal Server Error"
    }
  }
}

Can i do it this way or any other way?

Thanks in advance

Solved Solved
0 10 2,643
1 ACCEPTED SOLUTION

Firstly on your KVM Policy

1. You havent specified the mapidentifier - this is the name of your kvm

2. For your parameter, you have given it a ref of my_name.. a ref is looking for another variable called my_ref. I'm assuming this doesnt exist, instead you want to do

<Parameter>my_name</Parameter> where my_name is your KVM entry key

Once you've made these changes, test using trace to verify the policy sets a value for movie.director as you expect

On your AM Policy,

1. The type is set to request, this should be response if you want to return it in the response payload

2. Your payload will return a string of movie.director, not the variable - see templating https://docs.apigee.com/api-platform/reference/message-template-intro, ie it should be {movie.director}

3. You want to set the payload

ie

<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="Key-Value-Map-Operations-1" mapIdentifier="kvmname">
    <DisplayName>Key Value Map Operations-1</DisplayName>
    <Properties/>
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
    <Get assignTo="movie.director" index="1">
        <Key>
            <Parameter>name</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Set>
        <Payload>{movie.director}</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

View solution in original post

10 REPLIES 10

Firstly on your KVM Policy

1. You havent specified the mapidentifier - this is the name of your kvm

2. For your parameter, you have given it a ref of my_name.. a ref is looking for another variable called my_ref. I'm assuming this doesnt exist, instead you want to do

<Parameter>my_name</Parameter> where my_name is your KVM entry key

Once you've made these changes, test using trace to verify the policy sets a value for movie.director as you expect

On your AM Policy,

1. The type is set to request, this should be response if you want to return it in the response payload

2. Your payload will return a string of movie.director, not the variable - see templating https://docs.apigee.com/api-platform/reference/message-template-intro, ie it should be {movie.director}

3. You want to set the payload

ie

<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="Key-Value-Map-Operations-1" mapIdentifier="kvmname">
    <DisplayName>Key Value Map Operations-1</DisplayName>
    <Properties/>
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
    <Get assignTo="movie.director" index="1">
        <Key>
            <Parameter>name</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Set>
        <Payload>{movie.director}</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

Hello Dane, I'm doing the same kind of thing as I'm getting a response in JSON and want to save it in KVM but not the whole response.

My response is in this format:

{ "data":
{"access_token": "123"..,
"expires_in":"600",
"refresh_token":"456..."}
}

Here I only want to save the data of access_token in KVM.

Currently, my KVM looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="Key-Value-Map-Operations-1" mapIdentifier="PWC-token">
    <DisplayName>Key Value Map Operations-1</DisplayName>
    <Properties/>
    <Put overrride="true">
        <Key>
            <Parameter>token</Parameter>
        </Key>
        <Value ref="response.content"/>
    </Put>
    <Scope>environment</Scope>
</KeyValueMapOperations>

But the whole payload is getting saved and I only want to save access_token.

Any help on this would be grateful as I'm new to apigee

Hi @HIMADRI BHATTACHARJEE

Try this policy instead of the second one that you mentioned. Important: Attach this policy in the response flow.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Set>
        <Payload contentType="text/plain">{movie.director}</Payload>
    </Set>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

Note: I've set the IgnoreUnresolvedVariables flag to false an the content-type to text. You can alter them based on your actual requirement. You mentioned "print the variable". I assumed you want to return it in the response as part of the payload.

Tip: Use the "CODE" tool from the toolbar to format any xml/json content that you paste in the forum to make your Q&A more readable.

This is correct. Also, note

1. The way it's referring to the variable inside the payload

https://docs.apigee.com/api-platform/reference/message-template-intro

2. At the end, the AssignTo type is set to response.

i want to extract my_name and print it out...

"Print it out" - do you mean return it in the response payload or print it in the debug logs? Did you try out the corrections in your policy mentioned by dane and me? What's the current status? Only when you provide more information will you enable us to help you further.

The solution provided above is correct for proxy execution, you don't need to print the extracted variable explicitly. trace the call you will see the value for my_name flow variable. Yes, if you provide more information we would be able to help you further.

In addition to Claudio's response, there's the following issues with your KVM policy

1. It's missing a mapidentifier at the top - this is your KVM name.

2. It's trying to use a ref to identify the KVM parameter/key.

ie <Parameter ref="my_name"/>

This is looking for the variable my_name, to then use its value as the KVM key to look up. Instead you probably want

<Parameter>my_kvm_key</Parameter>

After updating your KVM policy, I'd also suggest testing with the trace tool to verify your policy is setting the variable as expected.

Thanks Dane. I missed the flaws in the OP's KVM policy.