Skip to content

Instantly share code, notes, and snippets.

@wsilveiranz
Created December 14, 2020 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wsilveiranz/ba789638aa3494de36d0ba0b728800af to your computer and use it in GitHub Desktop.
Save wsilveiranz/ba789638aa3494de36d0ba0b728800af to your computer and use it in GitHub Desktop.
POlicy to log Headers into Hookbin
<!--
IMPORTANT:
- Policy elements can appear only within the <inbound>, <outbound>, <backend> section elements.
- To apply a policy to the incoming request (before it is forwarded to the backend service), place a corresponding policy element within the <inbound> section element.
- To apply a policy to the outgoing response (before it is sent back to the caller), place a corresponding policy element within the <outbound> section element.
- To add a policy, place the cursor at the desired insertion point and select a policy from the sidebar.
- To remove a policy, delete the corresponding policy statement from the policy document.
- Position the <base> element within a section element to inherit all policies from the corresponding section element in the enclosing scope.
- Remove the <base> element to prevent inheriting policies from the corresponding section element in the enclosing scope.
- Policies are applied in the order of their appearance, from the top down.
- Comments within policy elements are not supported and may disappear. Place your comments between policy elements or at a higher level scope.
-->
<policies>
<inbound>
<!-- <check-header name="api-govt-nz-CorrelationID" failed-check-httpcode="400" failed-check-error-message="api-govt-nz-CorrelationId is missing in the request" ignore-case="true" />-->
<set-variable name="headerlogrequest" value="@{
var myStringBuilder = new StringBuilder();
bool first = true;
myStringBuilder.Append("{{\"direction\": \"request\"}");
myStringBuilder.AppendFormat("{{\"{0}\":\"{1}\"}}", "operation", context.Operation.Name);
foreach (KeyValuePair<string, string[]> pair in context.Request.Headers)
{
myStringBuilder.AppendFormat("{{\"{0}\":\"{1}\"}}", pair.Key, string.Join("-",pair.Value));
}
myStringBuilder.Append("}");
return myStringBuilder.ToString();
}" />
<send-one-way-request mode="new">
<set-url>{{Debug-Header-Logger-FrontDoor-Investigation}}</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-header name="x-operation-metadata" exists-action="override">
<value>@(string.Format("{0}-{1}",context.Operation.Name, "request"))</value>
</set-header>
<set-body>@(context.Variables.GetValueOrDefault<String>("headerlogrequest", ""))</set-body>
</send-one-way-request>
<check-header name="api-govt-nz-CorrelationID" failed-check-httpcode="400" failed-check-error-message="api-govt-nz-CorrelationId is missing in the request" ignore-case="true" />
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<set-variable name="headerlogresponse" value="@{
var myStringBuilder = new StringBuilder();
bool first = true;
myStringBuilder.Append("{{\"direction\": \"response\"}");
myStringBuilder.AppendFormat("{{\"{0}\":\"{1}\"}}", "operation", context.Operation.Name);
foreach (KeyValuePair<string, string[]> pair in context.Response.Headers)
{
myStringBuilder.AppendFormat("{{\"{0}\":\"{1}\"}}", pair.Key, string.Join("-",pair.Value));
}
myStringBuilder.Append("}");
return myStringBuilder.ToString();
}" />
<send-one-way-request mode="new">
<set-url>{{Debug-Header-Logger-FrontDoor-Investigation}}</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-header name="x-operation-metadata" exists-action="override">
<value>@(string.Format("{0}-{1}",context.Operation.Name, "response"))</value>
</set-header>
<set-body>@(context.Variables.GetValueOrDefault<String>("headerlogresponse", ""))</set-body>
</send-one-way-request>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment