Best practice about modify the request

evazquezl
Participant I

I want to modify the request, because some times use static values or maybe the request is very extensible. My idea is send a small request, with minimal data and add more properties and values.

For example, my flow is:

  • KVM Client, to extract static values from KVM.
  • Extract Variables: to extract values from Request Payload

Request is:

{
	"UserData" : "Mjk4MDMwJjIzMjMyMyY5OTk5OTkwMDE5MjUwNDI4",
	"NameParameters": "Mjk4MDMwJjIzMjMyMyY5OTk5OTkwMDE5MjUwNDI4"
}
  • Assign Message: to set Payload with values from Request and KVM Client.
    <Set>
        <Headers/>
        <QueryParams/>
        <FormParams/>
        <!-- <Verb>GET</Verb> -->
        <Path/>
        <Payload>
            {
                "MessageHeader": {
                    "CreationDateTime": "{request.queryparam.date}",
                    "TestDataIndicator": "false",
		            "SenderBusinessSystemID": "{request.queryparam.businessSystemId}",
		            "RecipientBusinessSystemID": "{request.queryparam.recepientbusinessId}"
                },
                "MessageContentHeader": {
            		"IdentificationParty": 
            		{ 
            			"CompanyID": "{request.queryparam.companyId}",
            			"CompanyDescription": "{request.queryparam.businessSystemId}"
            		}
            	},
            	"MessageContent": {
            		"RequestUri":"{request.queryparam.uriN2}",
            		"UserData" : "{UserData}",
            		"NameParameters": "{NameParameters}"
            	}
            }
        </Payload>
    </Set>

Is correct to use the politices this way?

Or is better to manipulate the request on Javascript policy?

I appreciate your comments.

Thank you!

1 1 887
1 REPLY 1

Good question. I like your approach - to accept a simple inbound request and then use the logic in Apigee Edge to build a more complicated request payload with "default" values possibly retrieved from the KVM. That's a useful pattern; We call it the "Facade" pattern sometimes.

ok, to answer your question.

First I think you should omit elements in the AssignMessage that you are not using. Do not include <FormParams/> or <Headers/> etc if you are not setting anything in those places.

Second, you should set the content-type of the payload. Like this:

  <Set>
   <Payload contentType='application/json'> ...

If you have successfully extracted values from the inbound payload to context variables named "UserData" and "NameParameters" , then the message template you have looks good.

This also assumes that the query parameters you are referencing are all present.

So everything looks good, and it will work nicely the way you have configured it.

you asked: Is it better to use AsignMessage or JS policies to manipulate the payload?

It depends.

It depends on what you are comfortable with. I like the template behavior of AssignMessage. It's easy to read and clear. JavaScript seems less clear to me, but in your case, you may have a different opinion. The performance will not be substantially different.