Newline character in KVM

Not applicable

Hi,

I have a value in KVM with '\n' a newline character. When I retrieve the value from KVM and pass it to Javascript it is not getting processed as newline.

When I give the same value as a string variable in javascript with '\n' the newline character is processsed and working fine.

I want to know a way when I retrieve the value from KVM the newline character is proccessed before I pass it to another library.

Thanks,

Chaitanya.

Solved Solved
1 3 901
1 ACCEPTED SOLUTION

Hi Chaitanya

I just tried this and ... it works for me!

I suspect you're having trouble encoding and storing the value.

I have stored in the KVM a value with a \n. I did this via the API.... When I invoke the API call, the string contains a slash character, followed by a n character. It looks like this:

curl -X POST $mgmtserver/v1/o/$org/e/$env/keyvaluemaps/$mapname/entries/test-key -H content-type:application/json -d '{
  "name" : "test-key",
  "value" : "test-value 20170803-144830\nThis is line #2\nThis is line #3"
}'

Then, in the API proxy, I include a KVM Get policy configured like so:

<KeyValueMapOperations name="KVM-Get-1" mapIdentifier="NonSecrets">
    <DisplayName>KVM-Get-1</DisplayName>
    <ExpiryTimeInSecs>10</ExpiryTimeInSecs>
    <Get assignTo="myVariable">
        <Key>
            <Parameter>test-key</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

And then in the response flow, I have an AssignMessage policy, configured like this:

<AssignMessage name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Remove>
        <Headers/>
    </Remove>
    <Set>
        <Payload contentType="text/plain">{myVariable}</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

The output is like this:

$ curl -i https://${org}-${env}.apigee.net/retrieve-kvm-multiline
HTTP/1.1 200 OK
Date: Thu, 03 Aug 2017 21:46:29 GMT
Content-Type: text/plain
Content-Length: 58
Connection: keep-alive
Server: Apigee Router


test-value 20170803-144830
This is line #2
This is line #3

This shows that multiple lines are in the retrieved value.

When I run a JS callout like this:

 var value = context.getVariable('myVariable');
 var lines = value.split('\n');
 context.setVariable('line_count', lines.length.toFixed(0));

...the line_count variable gets the value '3'. This shows that the JS callout "sees" the multiple lines.

There is one problem - the Apigee Edge UI does not properly show the newlines in the Web page.

5412-screenshot-20170803-145454.png

This is a UI bug. But at runtime it behaves properly.

View solution in original post

3 REPLIES 3

Hi Chaitanya

I just tried this and ... it works for me!

I suspect you're having trouble encoding and storing the value.

I have stored in the KVM a value with a \n. I did this via the API.... When I invoke the API call, the string contains a slash character, followed by a n character. It looks like this:

curl -X POST $mgmtserver/v1/o/$org/e/$env/keyvaluemaps/$mapname/entries/test-key -H content-type:application/json -d '{
  "name" : "test-key",
  "value" : "test-value 20170803-144830\nThis is line #2\nThis is line #3"
}'

Then, in the API proxy, I include a KVM Get policy configured like so:

<KeyValueMapOperations name="KVM-Get-1" mapIdentifier="NonSecrets">
    <DisplayName>KVM-Get-1</DisplayName>
    <ExpiryTimeInSecs>10</ExpiryTimeInSecs>
    <Get assignTo="myVariable">
        <Key>
            <Parameter>test-key</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

And then in the response flow, I have an AssignMessage policy, configured like this:

<AssignMessage name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Remove>
        <Headers/>
    </Remove>
    <Set>
        <Payload contentType="text/plain">{myVariable}</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

The output is like this:

$ curl -i https://${org}-${env}.apigee.net/retrieve-kvm-multiline
HTTP/1.1 200 OK
Date: Thu, 03 Aug 2017 21:46:29 GMT
Content-Type: text/plain
Content-Length: 58
Connection: keep-alive
Server: Apigee Router


test-value 20170803-144830
This is line #2
This is line #3

This shows that multiple lines are in the retrieved value.

When I run a JS callout like this:

 var value = context.getVariable('myVariable');
 var lines = value.split('\n');
 context.setVariable('line_count', lines.length.toFixed(0));

...the line_count variable gets the value '3'. This shows that the JS callout "sees" the multiple lines.

There is one problem - the Apigee Edge UI does not properly show the newlines in the Web page.

5412-screenshot-20170803-145454.png

This is a UI bug. But at runtime it behaves properly.

Thanks dino. I tried storing the values in the KVM using the mangement APIs instead of the UI and it worked.

Right on! Thanks for letting me know. I'll get the UI bug logged, and see if I can have it fixed quickly.

Edit

Ok, I've filed bug #64612551 to track this UI problem.