Conditional Flow - convert body field to query param

Not applicable

Hello,

My proxy is: https://my-org.net/myapi/symptoms and my POST body is

{ "name": "stomach ache", "createdDate" : {".sv":"timestamp"} }

I'd like my URL to be https://my-org.net/myapi/symptoms?description='stomach ache'

What is the best way to achieve this? Shall I use Javascript to parse out the pathsuffix and then substitute the symptom in the body above? If so, what is the variable for the body and how do I pass it in the JS?

Thanks.

Solved Solved
0 7 328
1 ACCEPTED SOLUTION

Hi @Danielle Laforte

If I understand your question correctly, you want to expose your proxy API to be https://my-org.net/myapi/symptoms?description="stomach ache" and you want Apigee Edge to convert this GET call to a POST call with a request body sent to your backend. The sample request body is

{
  "name":"stomach ache",
  "createdDate":{
    ".sv":"timestamp"
  }
}

where it picked the query param - description and used it as name within the request payload. The reason I clarify is, you post title says "convert body filed to query param" but your post description seems to be the opposite. At least thats how I understood.

However, If my above assumption is right - You can easily do this by using an Assign Message policy, something like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-Set-Request-Body">
    <DisplayName>AM-Set-Request-Body</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="application/json" variablePrefix="@" variableSuffix="#">
           {
              "name":"@request.queryparam.description#",
              "createdDate":{
                ".sv":"timestamp"
              }
            }
        </Payload>
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

This will create a request body and also convert the request verb from GET to POST. Use this within your Request flow

View solution in original post

7 REPLIES 7

Hi @Danielle Laforte

If I understand your question correctly, you want to expose your proxy API to be https://my-org.net/myapi/symptoms?description="stomach ache" and you want Apigee Edge to convert this GET call to a POST call with a request body sent to your backend. The sample request body is

{
  "name":"stomach ache",
  "createdDate":{
    ".sv":"timestamp"
  }
}

where it picked the query param - description and used it as name within the request payload. The reason I clarify is, you post title says "convert body filed to query param" but your post description seems to be the opposite. At least thats how I understood.

However, If my above assumption is right - You can easily do this by using an Assign Message policy, something like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-Set-Request-Body">
    <DisplayName>AM-Set-Request-Body</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="application/json" variablePrefix="@" variableSuffix="#">
           {
              "name":"@request.queryparam.description#",
              "createdDate":{
                ".sv":"timestamp"
              }
            }
        </Payload>
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

This will create a request body and also convert the request verb from GET to POST. Use this within your Request flow

I want to thank you for this answer.

You are welcome @Danielle Laforte. Please reach out if you need anything

Hi Sai Saran Vaidyanathan,

Everything is working except for some reason it isn't substituting the variable correctly and my DB entry looks like this. The name is supposed to be substituted using this: "@request.queryparam.description#" so for this https://my-org.net/myapi/symptoms?description="stomach ache" the name should be stomach ache. I am using your AM.

6170-screen-shot-2017-12-18-at-14244-pm.png

I am also using this AM:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-NoCopyPath">
    <AssignVariable>
        <Name>target.copy.pathsuffix</Name>
        <Value>false</Value>
    </AssignVariable>
</AssignMessage>

and I am using this JS:

var pathsuffix = context.getVariable('proxy.pathsuffix');
var originalTargetUrl = context.getVariable('target.url');
context.setVariable('target.url', originalTargetUrl + pathsuffix + '.json');

I'm wondering if I am cancelling out the name variable with one of these.

Basically I want this raw: POST https://daniellelaforte-eval-test.apigee.net/myapi/symptoms.json with this body:

 {
              "name":"headache",
              "createdDate":{
                ".sv":"timestamp"
              }
            }

to be: POST

https://daniellelaforte-eval-test.apigee.net/myapi/symptoms?description='headache'

If you could take a look, I'd really appreciate it.

@Danielle Laforte If I understand this again

Your request is

curl -X POST \
  https://daniellelaforte-eval-test.apigee.net/myapi/symptoms.json \
  -H 'content-type: application/json' \
  -d '{
              "name":"headache",
              "createdDate":{
                ".sv":"timestamp"
              }
            }'

to be converted to

curl -X POST \
  'https://daniellelaforte-eval-test.apigee.net/myapi/symptoms?description=headache' \
  -H 'content-type: application/json'

Is that correct ? Do you still need the request payload to be sent to the target ?

OK, I see my problem. The two AMs are conflicting. How do I just run your AM when I call the symptoms resource?

If you can reply to my question, I can help with the code