Extension Policy: Publishing Request Payload to PubSub topic

I am working on a requirement to publish data to pubsub topic on GCP.

I am able to connect to pubsub topic using extension policy.

I need to publish the request payload which is a json object to PubSub topic .

I tried below with extension policy:

<Input>{"message": {request.content}}</Input>

returns an error:data.message should be string,object

whereas I am checking if it is a valid json and this returns an error as mentioned above.

It works when I try below but adds escaping characters(\r\n\t)

<Input>{"message": {escapeJson(request.content)}}</Input>

Dino-at-Google could you please help me with this?

Thanks

Sonal

0 3 300
3 REPLIES 3

Hi Sonal,

can you elaborate on what you mean by "but adds escaping characters"?

Can you give me a specific example, or screenshots of the Trace UI, with and without that option?

or maybe a downloaded trace session, so I can look myself?

If you don't care to share the trace , then you yourself can use AssignMessage to look at the result of {request.content} vs {escapeJson(request.content)} , prior to invoking the Extension policy.

Thanks Dino-at-Google for your response.

Issue I am facing is as below:

Case 1: Extension policy setting is <Input>{"message": {request.content}}</Input>

send json {"version":"1.0","type":"update"} through postman and this works fine and data gets published to the topic .

send json array [ {"version":"1.0","type":"update"},{"version":"2.0","type":"delete"}]

I am receiving error as data.message should be string,object

Case 2: Extension policy setting is <Input>{"message": "{escapeJson(request.content)}"}</Input>

send json {"version":"1.0","type":"update"}

send json array [ {"version":"1.0","type":"update"},{"version":"2.0","type":"delete"}]

Both the above gets published to Pub Sub topic (case2)but I see escape characters added to the jsons:

9851-capture.png

I need the data as I am sending to be added to pub sub topic without escape characters.

Could you please help on this?

Thanks

Sonal

I see, ok.

The error data.message should be string,object is coming from the pubsub extension, is that right?

What I infer from that is that the pubsub extension is expecting a form like

 { "message" : { "prop1" : "value1", "prop2" : "value2" ... } }

or

 { "message" : "string-goes-here" }

and you are passing

 { "message" : [ { ...}, {...} ... ] }

...which is an array. That is unacceptable. I am unsure if it is the Apigee extension which is rejecting the message, or the cloud pubsub endpoint. But no matter, in any case it is unacceptable. So you must format your message in one of the two required forms: String, or Object. Not array.

documentation

One option is to always wrap the request.content in a parent element.

<Input>{"message": {"payload" : {request.content}}}</Input>

Regardless whether the request.content represents an array or an object, or even a primitive (like a quoted string, the number 2 or the word 'true'), the "message" will always be a JSON object.

Another option is to encode the request.content in some way. escapeJson is one way. Base64 encoding is another way. Any way to produce a single string will work.