Azure APIM policy: Set default value for requestParameter - azure

I want to set a hardcoded default value for my #RequestParam variable for sometime.
Eg: UI sends--> url.com/v1/apiName?fruits=apple,mango
we want --> url.com/v1/apiName?fruits=apple,mango,banana
Reason for this: UI calls this API they send 2 default values(unfortunately hardcoded) we introduced 1 new value. Their deployment will take time, so for time being, we want to override their inputs.
New to APIM, so need some help.
Thanks in advance.

Have you tried the "set-query-parameter"-policy?
<set-query-parameter name="fruits" exists-action="override">
<value>apple,mango,banana</value>
</set-query-parameter>

If you want to rewrite the value, first you need to get the value and then add your part.
You could use "set-query-parameter" snippet method to do this.
<set-query-parameter name="fruits" exists-action="override">
<value>#{
string fruits = context.Request.OriginalUrl.Querys["fruits"]; // apple mango
return $"{fruits},banana"; // concatenation
}</value>
</set-query-parameter>
This way you'll get the original value from the request and add yours to it.
2 urls you'll need working with api management + some small knowledge in C# (Google it's enough for that)
https://learn.microsoft.com/en-us/azure/api-management/api-management-policy-expressions
https://learn.microsoft.com/en-us/azure/api-management/api-management-advanced-policies

Related

Read Named values from the context in Azure Apim

I'm defining a inbound policy and trying to construct a URL from 2 components. I know how to get these values 1 but 1 but when I have to combine them into one cant figure out the syntax.
First I call a api and save the response
<send-request mode="copy" response-variable-name="createdUser" timeout="20" ignore-error="false">
<set-url>{{Systems.One.Endpoint}}/services/rest</set-url>
</send-request>
I can trace my response is ok like this
<trace source="Contact Id">#(((IResponse)context.Variables["createdUser"]).Body.As<JObject>()["contactId"])</trace>
But I dont know how to create a url from the {{System.One.Endpoint}} and the #(((IResponse)context.Variables["createdUser"]).Body.As()["contactId"])
This here does not work
<send-request mode="copy" response-variable-name="createPeriod" timeout="seconds" ignore-error="true">
<set-url>
{{System.Two.Endpoint}}/api/#(((IResponse)context.Variables["createPeriod"]).Body.As<JObject>()["contactId"])/createperiod/{{DefaultPeriodlength}}
</set-url>
Everything after the # is just being set as string and not loaded from the actual value form the variable
How can I combine these two values?
Policy expressions can only be used to produce whole value for element/attribute. Named values in contract can be used to represent any part of any attribute/element value, and are inserted into policy prior to it being analyzed/executed.So something like this:
<set-url>#("{{System.Two.Endpoint}}/api/" + ((IResponse)context.Variables["createPeriod"]).Body.As<JObject>()["contactId"].ToString() + "/createperiod/{{DefaultPeriodlength}}")</set-url>
or if you prefer string interpolation:
<set-url>#($"{{System.Two.Endpoint}}/api/{((IResponse)context.Variables["createPeriod"]).Body.As<JObject>()["contactId"]}/createperiod/{{DefaultPeriodlength}}")</set-url>

How to change canonicalization method

does anyone know how to change the CanonicalizationMethod property in xadesjs? I know it is using xmldsigjs underneath but I cannot find a solution to change it. I mean the CanonicalizationMethod for the whole SignedInfo property (not the transforms for the references). Currently (by default) I get a document that has CanonicalizationMethod set to C14N:
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
I want to change it to EXC-C14N:
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
Is it even possible?
Ok, I've found a solution. In case anybody needs it, you can directly assign it with:
let signedXml = new XAdES.SignedXml();
signedXml.XmlSignature.SignedInfo.CanonicalizationMethod.Algorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";

restify.js route endpoint conflict with parameter

My code is like below:
server.get('/currency/:code', currency.find);
server.get('/currency/rates', currency.rate_getall);
Whenever I try to reach [/rates] endpoint, the server will assume I am passing parameter to '/currency/:code' route. How can I fix this? Thank you.
Ryan
If you can I would consider changing up your rest interface just a little.
server.get('/currency/:code', currency.find);
server.get('/currency/rates/:type', currency.rate);
That way it solves your initial problem and allows for flexibility in the future if you just want to return a rate for a particular currency.
Inside your currency.rate function you could check for either an id or the literal 'all' and return what is appropriate.

How do you insert the same random variable into multiple soapui testcase requests?

I may be going about this in the completely wrong way, but how do I pass a dynamic variable to a bunch of requests within the same testsuite in SoapUI?
My first test step is a Groovy script. I need to generate a random account name, and then use it in all my other requests. There are about 20 other requests. I initially thought I could just loop the testsuite, but it is not working.
This is my groovy script at the beginning:
Random random = new Random()
def randUserAccount = "testAccount"
int max = 100000
randnum = random.nextInt(max+10000)
randUserAccount += randnum
log.info " Creating account: $randUserAccount"
Then in each request step, I have things like this:
<ns:CreateAccountRequest>
<accountID>${randUserAccount}</accountID>
...
or
<ns:PurchaseRequest>
<accountID>${randUserAccount}</accountID>
...
The account is null when I actually send it, and of course that gives errors on the server side. How do I really get the variable to persist across all the requests in the testsuite?
Thanks in advance for any hints!
You can use the context, I believe. You can definitely use it between requests in a test, but I also think it will work between tests in a suite.
context.setProperty("randUserAccount", randUserAccount)
Then use the syntax you specified in the actual requests.
Let me know if this doesn't work. You can also use 'properties' to do this, but it is a little more work.
or you can create a variable in property then set the value through set property as mentioned above..
for every tag jus right click and check the your project varaible it will automatically insert the code..
Hope it help

WSO2 - Using get-property() function in Property/Xquery Mediators

Our current service has 7 operations. when writing an outbound xquery "local entry" in wso2, we're trying to retrieve the name of the current operation being executed (how can this be so difficult?).
After reading what i could find in wso2's documentation. it appears as if we need to set up both a Property and an Xquery mediator. supposedly the property mediator would pull the value doing something like get-property('OperationName') and then this would be referenced and passed thru the Xquery mediator.
The other idea was that we needed to define it as a variable in the "Local Registry entry definitions" and than it would be around at all parts of the sequence.
I've tried for 2 days but haven't quite got it.
Please tell me what I'm missing...
Did you try the following xquery sample[1]? I modified the query mediator to get the operation name as follows.
<variable xmlns:ax21="http://services.samples/xsd" xmlns:m0="http://services.samples" name="code" expression="get-property('OperationName')" type="STRING" />
this worked fine. I could see the getQuote in the response message.
[1] http://wso2.org/project/esb/java/4.0.2/docs/samples/advanced_mediation_samples.html#Sample390

Resources