|
1 | 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,for some special tier consumers, |
4 |
| - we would still like to serve the request if there was a quota error but this time with a low-end backend. |
5 |
| - |
6 |
| - The "example-policy-name" string is in the when condition is just for example and needs to be replaced with the actual policy name accordingly. |
| 2 | + This policy sample demonstrates a pattern of replaying a request. |
| 3 | + For eg: one scenario could be when we are using quota and rate limit policies and for some special tier consumers, |
| 4 | + we would like to serve the request even if there was a quota error but with a different backend. |
| 5 | + In such cases, we can replay the request to a different backend with the same request headers and body. |
| 6 | + The "example-policy-name" string in the when condition is just for example and needs to be replaced with the actual policy name accordingly. |
7 | 7 |
|
8 | 8 | Important Note:
|
9 |
| - Although this pattern works, we would do end up having 2 log entries now for the same request, since the request is now being replayed. |
| 9 | + Although this pattern works, an important point to note here is since we are now replaying the request |
| 10 | + we would end up having 2 log entries for the same request in the APIM logs. |
10 | 11 | -->
|
11 | 12 | <policies>
|
12 | 13 | <inbound>
|
|
25 | 26 | <set-variable name="replay-url" value="@("http://" + context.Request.Headers.GetValueOrDefault("X-Original-Host") + context.Request.Headers.GetValueOrDefault("X-Original-Url"))" />
|
26 | 27 | <set-variable name="original-error-source" value="@(context.LastError.Source)" />
|
27 | 28 | <set-variable name="original-error-reason" value="@(context.LastError.Reason)" />
|
28 |
| - <!-- We replay the same request again to APIM --> |
| 29 | + |
| 30 | + <!-- Following snipped demonstrates the replya pattern, |
| 31 | + For this, we are using send request's "copy" mode which would copy the original request's headers and body accoridngly. |
| 32 | + Policy Documentation link https://learn.microsoft.com/en-us/azure/api-management/send-request-policy#attributes |
| 33 | + --> |
| 34 | + <!-- Note: |
| 35 | + As per the policy documentation, the send request method will copy the request headers and body accordingly |
| 36 | + but we should always validate this and we can expclitly set the the request body and headers as needed. |
| 37 | + --> |
29 | 38 | <send-request mode="copy" response-variable-name="replay-response" timeout="60" ignore-error="false">
|
30 | 39 | <set-url>@(context.Variables.GetValueOrDefault<string>("replay-url"))</set-url>
|
31 |
| - <set-method>POST</set-method> |
32 | 40 | <!-- While replaying the request this time, we might need to pass some additional information
|
33 | 41 | this time in our request with corresponding logic in the inbound section to process such requests
|
34 |
| - The string "skip-example-policy-error" here is just as an example --> |
| 42 | + The string "skip-example-policy-error" here is just an example of additional header we are sending here |
| 43 | + --> |
35 | 44 | <set-header name="skip-example-policy-error" exists-action="override">
|
36 | 45 | <value>true</value>
|
37 | 46 | </set-header>
|
38 | 47 | </send-request>
|
39 |
| - <!-- THIS IS AN IMPORTANT STEP i.e. the response captured in policy variable "response-variable-name" defined above has to be explicity sent back --> |
| 48 | + <!-- THIS IS AN IMPORTANT STEP i.e. the response captured in the send-request policy attribute |
| 49 | + "response-variable-name" defined above has to be explicity sent back |
| 50 | + --> |
40 | 51 | <return-response response-variable-name="replay-response">
|
41 | 52 | <!-- Any additional headers like below can further be added as part of response tracking -->
|
42 | 53 | <set-header name="x-request-replayed" exists-action="override">
|
|
0 commit comments