Trying to use E4X to replace an XML attribute, I see an error in the Proxy Editor UI

Hi,

I want to replace an attribute from areaId=123 to areaId=456 before sending to target server. I have gone through below documentation and Q/A but I am getting error for '@' symbol

https://docs.apigee.com/api-platform/reference/javascript-object-model#contextobjectreference-contex...

https://www.googlecloudcommunity.com/gc/Apigee/How-to-modify-XML-request-payload-using-Javascript/td...

https://www.googlecloudcommunity.com/gc/Apigee/How-to-replace-attribute-in-xml-with-new-value/td-p/4...

I am getting below error

apigeeError.png

does Apigee X support JavaScript object model?

 

Solved Solved
0 2 131
1 ACCEPTED SOLUTION

The problem you're seeing is one of the Javascript parser in the Apigee UI. But that should not affect the runtime behavior.  If you want, you can avoid the problem by using different syntax: 

var message = context.getVariable('message');
if (message && message.content) {
  var xml = new XML(message.content);

  // modify the value
  xml['@AreaId'] = '456';

  // replace the content of the message with the modified version
  message.content = xml.toXMLString();
}

This works for me in Apigee X

$ curl -i $endpoint/e4x-modify-attribute/existing-doc
HTTP/2 200
traceparent: 00-4a9c22f671725c09c5b21bb7367d5034-e3abeae76df3cbf6-00
grpc-trace-bin: AABKnCL2cXJcCcWyG7c2fVA0AeOr6udt88v2AgA
content-type: text/xml
apiproxy: e4x-modify-attribute r1
x-request-id: 05b9b7cd-e4a2-4e4e-a208-20abadab1767
content-length: 70
date: Thu, 30 Jun 2022 17:03:23 GMT
via: 1.1 google, 1.1 google
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

<Area AreaId="123">
  <City>city</City>
  <State>state</State>
</Area>

$ curl -i $endpoint/e4x-modify-attribute/with-changes
HTTP/2 200
traceparent: 00-4b3c6856813f493251280d13ca710740-32d5ca20ffe49bb1-00
grpc-trace-bin: AABLPGhWgT9JMlEoDRPKcQdAATLVyiD/5JuxAgA
content-type: text/xml
apiproxy: e4x-modify-attribute r1
x-request-id: f1a719bc-ecff-4c66-9d98-581af4a74cac
content-length: 70
date: Thu, 30 Jun 2022 17:03:54 GMT
via: 1.1 google, 1.1 google
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

<Area AreaId="456">
  <City>city</City>
  <State>state</State>
</Area>

 

 

View solution in original post

2 REPLIES 2

The problem you're seeing is one of the Javascript parser in the Apigee UI. But that should not affect the runtime behavior.  If you want, you can avoid the problem by using different syntax: 

var message = context.getVariable('message');
if (message && message.content) {
  var xml = new XML(message.content);

  // modify the value
  xml['@AreaId'] = '456';

  // replace the content of the message with the modified version
  message.content = xml.toXMLString();
}

This works for me in Apigee X

$ curl -i $endpoint/e4x-modify-attribute/existing-doc
HTTP/2 200
traceparent: 00-4a9c22f671725c09c5b21bb7367d5034-e3abeae76df3cbf6-00
grpc-trace-bin: AABKnCL2cXJcCcWyG7c2fVA0AeOr6udt88v2AgA
content-type: text/xml
apiproxy: e4x-modify-attribute r1
x-request-id: 05b9b7cd-e4a2-4e4e-a208-20abadab1767
content-length: 70
date: Thu, 30 Jun 2022 17:03:23 GMT
via: 1.1 google, 1.1 google
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

<Area AreaId="123">
  <City>city</City>
  <State>state</State>
</Area>

$ curl -i $endpoint/e4x-modify-attribute/with-changes
HTTP/2 200
traceparent: 00-4b3c6856813f493251280d13ca710740-32d5ca20ffe49bb1-00
grpc-trace-bin: AABLPGhWgT9JMlEoDRPKcQdAATLVyiD/5JuxAgA
content-type: text/xml
apiproxy: e4x-modify-attribute r1
x-request-id: f1a719bc-ecff-4c66-9d98-581af4a74cac
content-length: 70
date: Thu, 30 Jun 2022 17:03:54 GMT
via: 1.1 google, 1.1 google
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

<Area AreaId="456">
  <City>city</City>
  <State>state</State>
</Area>

 

 

Thanks @dchiesa1 !!!