Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joewashington75/68bcf674124f4e1b77d96cda26ba8542 to your computer and use it in GitHub Desktop.
Save joewashington75/68bcf674124f4e1b77d96cda26ba8542 to your computer and use it in GitHub Desktop.
APIM send message to Azure Service Bus when backend API is down
<policies>
<inbound>
<base />
<set-backend-service base-url="https://yourapihere.com" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<choose>
<when condition="@(context.Response.Body.As<string>(preserveContent:true).Contains("response to check for and perform action"))">
<send-one-way-request mode="copy">
<set-url>https://{{service-bus-name}}.servicebus.windows.net/{{service-bus-queue}}/messages</set-url>
<set-header name="Content-Type" exists-action="override">
<value>vnd.microsoft.servicebus.yml</value>
</set-header>
<set-header name="Authorization" exists-action="override">
<value>{{service-bus-shared-access-signature}}</value>
</set-header>
<set-header name="BrokerProperties" exists-action="override">
<value>@{
var json = new JObject();
json.Add("MessageId", context.RequestId);
json.Add("Label", "");
return json.ToString(Newtonsoft.Json.Formatting.None);
}</value>
</set-header>
<set-body>@{
JObject json = context.Request.Body.As<JObject>(preserveContent: true);
return JsonConvert.SerializeObject(json);
}</set-body>
</send-one-way-request>
<set-status code="201" reason="Created" />
<set-body template="liquid">
{
"message": "Created"
}
</set-body>
</when>
</choose>
</outbound>
<on-error>
<base />
<choose>
<when condition="@(context.Response.StatusCode >= 400 && context.Response.StatusCode != 401)">
<send-one-way-request mode="copy">
<set-url>https://{{service-bus-name}}.servicebus.windows.net/{{service-bus-queue}}/messages</set-url>
<set-header name="Content-Type" exists-action="override">
<value>vnd.microsoft.servicebus.yml</value>
</set-header>
<set-header name="Authorization" exists-action="override">
<value>{{service-bus-shared-access-signature}}</value>
</set-header>
<set-header name="BrokerProperties" exists-action="override">
<value>@{
var json = new JObject();
json.Add("MessageId", context.RequestId);
json.Add("Label", "");
return json.ToString(Newtonsoft.Json.Formatting.None);
}</value>
</set-header>
</send-one-way-request>
<set-body template="liquid">
{
"message": "Created"
}
</set-body>
<set-status code="201" reason="Created" />
</when>
</choose>
</on-error>
</policies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment