Can we use set-query-parameter policy inside send-request policy? -No


Several times you come across a situation where you should use send-request policy to make use of an external service to perform complex processing functions and return data to the API management service that can be used for further policy processing.

The send-request policy looks like this:


<send-request mode="new" response-variable-name="tokenstate" timeout="20" ignore-error="true">
<set-url>https://microsoft-apiappec990ad4c76641c6aea22f566efc5a4e.azurewebsites.net/introspection</set-url>
<set-method>POST</set-method>
<set-header name="Authorization" exists-action="override">
<value>basic dXNlcm5hbWU6cGFzc3dvcmQ=</value>
</set-header>
<set-header name="Content-Type" exists-action="override">
<value>application/x-www-form-urlencoded</value>
</set-header>
<set-body>@($"token={(string)context.Variables["token"]}")</set-body>
</send-request>

Now if you want to use query string parameters in the URL under set-url policy used above using set-query-parameter policy then it is not allowed and you will not be able to even save your configured policy. Generally, we get confused and try to make use of this policy inside send-request policy but set-query-parameter policy is used to add, replace value of, or deletes request query string parameter. It Can be used to pass query parameters expected by the backend service which are optional or never present in the request according to this link https://msdn.microsoft.com/library/azure/7406a8ce-5f9c-4fae-9b0f-e574befb2ee9#SetQueryStringParameter
Now the question is how to pass query string parameters? It’s very simple and logical, we can make use of APIM variables in this scenario.

Firstly, setting the variables with some values:

<set-variable name="fromDate" value="@(context.Request.Url.Query["fromDate"].Last())">
<set-variable name="toDate" value="@(context.Request.Url.Query["toDate"].Last())">

And secondly making use of these variables inside send-request policy:

<send-request mode="new" response-variable-name="revenuedata" timeout="20" ignore-error="true">
<set-url>@($"https://accounting.acme.com/salesdata?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")"</set-url>
<set-method>GET</set-method></send-request>

Refer this link for more details https://github.com/Microsoft/azure-docs/blob/master/articles/api-management/api-management-sample-send-request.md

Please let me know your queries.

Happy Coding!


Comments (0)

Skip to main content