Skip to content

Commit 7180b51

Browse files
committed
Policy example for Replaying request
1 parent 1e57b4d commit 7180b51

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!--
2+
This policy sample demonstrates a pattern of replaying the request.
3+
For eg: One scenario could be when we are using quota and rate limit policies,
4+
for some special tier consumers, we would still like to serve the request if there
5+
was a quota error but this time with a low-end backend.
6+
7+
The "example-policy-name" string is in the when condition is just for example
8+
and needs to be replaced with the actual policy name accordingly.
9+
10+
Important Note:
11+
Altough this pattern works, we would do end up having 2 log entries now
12+
for the same request, since the request is now being replayed.
13+
-->
14+
<policies>
15+
<inbound>
16+
<base />
17+
</inbound>
18+
<backend>
19+
<base />
20+
</backend>
21+
<outbound>
22+
<base />
23+
</outbound>
24+
<on-error>
25+
<base />
26+
<choose>
27+
<when condition="@(context.LastError.Source == "example-policy-name")">
28+
<set-variable name="replay-url" value="@("http://" + context.Request.Headers.GetValueOrDefault("X-Original-Host") + context.Request.Headers.GetValueOrDefault("X-Original-Url"))" />
29+
<set-variable name="original-error-source" value="@(context.LastError.Source)" />
30+
<set-variable name="original-error-reason" value="@(context.LastError.Reason)" />
31+
<!-- We replay the same request again to APIM -->
32+
<send-request mode="copy" response-variable-name="replay-response" timeout="60" ignore-error="false">
33+
<set-url>@(context.Variables.GetValueOrDefault<string>("replay-url"))</set-url>
34+
<set-method>POST</set-method>
35+
<!-- While replaying the request this time, we might need to pass some additional information
36+
this time in our request with corresponding logic in the inbound section to process such requests
37+
The string "skip-example-policy-error" here is just as an example -->
38+
<set-header name="skip-example-policy-error" exists-action="override">
39+
<value>true</value>
40+
</set-header>
41+
</send-request>
42+
<!-- THIS IS AN IMPORTANT STEP i.e. the response captured in policy variable "response-variable-name" defined above has to be explicity sent back -->
43+
<return-response response-variable-name="replay-response">
44+
<!-- Any additional headers like below can further be added as part of response tracking -->
45+
<set-header name="x-request-replayed" exists-action="override">
46+
<value>true</value>
47+
</set-header>
48+
<set-header name="x-original-error-source" exists-action="override">
49+
<value>@(context.Variables.GetValueOrDefault<string>("original-error-source"))</value>
50+
</set-header>
51+
<set-header name="x-original-error-reason" exists-action="override">
52+
<value>@(context.Variables.GetValueOrDefault<string>("original-error-reason"))</value>
53+
</set-header>
54+
</return-response>
55+
</when>
56+
<otherwise>
57+
<!-- Any additional headers like below can further be added as part of response tracking -->
58+
<set-header name="x-request-replayed" exists-action="override">
59+
<value>"false"</value>
60+
</set-header>
61+
</otherwise>
62+
</choose>
63+
</on-error>
64+
</policies>

0 commit comments

Comments
 (0)