apigee cloudfunction payload not received as json

Hi,

I am trying to send a json object as post parameter to APIGEE proxy as follows

curl -X POST -H "Content-type:application/json" -d {"a":"b"} https://xyz.apigee.net/fhir-get-cloudfunction

I have cloud function extension configured and working fine if i hard code the payload in side the cloud function policy as

"payload" : {"a":"b"}

Not sure how to capture the input from post request and pass as payload. I tried

1) "payload" : {{request.content}}-------------not working

2) "payload" : {"param":{request.content}}-------------not working

3) "payload" : {"param":"{request.content}"}---------it works but in cloud functions i see the input as {"param":"a:b"}

how can i convert "a:b" to a json object for further processing in cloudfunction?

Seems like i am not sending the parameter properly from extension.

Solved Solved
0 8 320
2 ACCEPTED SOLUTIONS

Have you tried

"payload" : {request.content}

?

The notation {variablename} is probably a message template. It replaces the reference with the value contained in the named variable. Since your request.content variable contains {"a":"b"}, then ... the result should be

"payload" : {"a": "b"}

...which is what you want I think.

Also, re: "not working". Just a suggestion for the future so that you can self-diagnose these things. Examine the result of the message template. If you can't do it directly, then infer the result by using an additional AssignMessage / AssignVariable, just for diagnostic purposes.

<AssignMessage name='AM-Diagnostics'>
  <AssignVariable> 
    <Name>for_diagnostics_only</Name>
    <Template>
      "payload" : {request.content}
    </Template>
  </AssignVariable> 

</AssignMessage>

If you run your proxy with a trace session you should be able to see the result of the above. I don't know what Cloud Functions is expecting exactly, but maybe you do. You can use the AM-Diagnostics policy as a way to evaluate whether you are generating what you intend or desire to generate in the template.

good luck.

View solution in original post

Not applicable

I tried this one in one assign message policy as below and it works for me.

<?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="application/json">
            { "payload" : {request.content} }
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

and the new payload going to by backend is

{
	"payload": {
		"a": "b"
	}
}

View solution in original post

8 REPLIES 8

There's an example here of interacting with a cloud function using a service callout. This gives you more control over how you can interact with your cloud function

https://github.com/dknezicd/memorystore-blacklist

this example does not address my question. I am not having any authentication issue. My issue is related to sending json object as post parameter

it does address your question because it gives you a pattern for interacting with a cloud function where you can customize the payload and http verb

Have you tried

"payload" : {request.content}

?

The notation {variablename} is probably a message template. It replaces the reference with the value contained in the named variable. Since your request.content variable contains {"a":"b"}, then ... the result should be

"payload" : {"a": "b"}

...which is what you want I think.

Also, re: "not working". Just a suggestion for the future so that you can self-diagnose these things. Examine the result of the message template. If you can't do it directly, then infer the result by using an additional AssignMessage / AssignVariable, just for diagnostic purposes.

<AssignMessage name='AM-Diagnostics'>
  <AssignVariable> 
    <Name>for_diagnostics_only</Name>
    <Template>
      "payload" : {request.content}
    </Template>
  </AssignVariable> 

</AssignMessage>

If you run your proxy with a trace session you should be able to see the result of the above. I don't know what Cloud Functions is expecting exactly, but maybe you do. You can use the AM-Diagnostics policy as a way to evaluate whether you are generating what you intend or desire to generate in the template.

good luck.

Hi Dino,

when i try

"payload" : {request.content}

the cloud function policy refuses to accept it as valid json input

I tried to include AssignMessage and I the value of for_diagnostics_only is being set as

"payload" : {a:b}

Not sure why the double quotes are getting dropped. At this time this has nothing to do with cloudfunction because I am losing my input json before even able to call cloudfunction.

Is there any other mechanism to handle this. I tried JSON.stringify as well to see if there is something special about handling of json objects at apigee but it looks like it always like to strip off doublequotes whether it is in json object or not

Not applicable

I tried this one in one assign message policy as below and it works for me.

<?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="application/json">
            { "payload" : {request.content} }
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

and the new payload going to by backend is

{
	"payload": {
		"a": "b"
	}
}

I realized that the issue was with my curl command. The double quotes were gone even before the requests hits apigee.

Yup - that's the kind of problem we all experience from time to time. The use of AssignMessage for diagnostics purposes can help figure out these problems. . . .

I'm glad you sorted it out.