Dump Everything from 'context.Request'

Q

How to build a test service operation to dump everything from the "context.Request" object in the response in Azure API Policy?

✍: FYIcenter.com

A

If you want to build a test service operation to dump everything from the request to the response with an Azure API policy, you can follow this tutorial:

1. Go to the publisher portal and create a new service on Azure called "Test API" with these settings:

Web API URL: http://fyi.azure-api.net/test
Web service URL: http://echoapi.cloudapp.net/api

2. Create a new operation in the service called "Dump Request" with these settings:

HTTP Verb: POST
URL Template: /customers/{cid}/orders/{oid}/?date={date}
Rewrite URL Template: /newLocation

3. Enter the following policy for this service operation:

<policies>
  <inbound>
    <base />
    <rewrite-uri template="/newLocation" />
  </inbound>
  <backend>
        <!-- no call to backend -->
  </backend>
  <outbound>
    <set-body>@{
    var res = "Request Received:\n";
  var api = context.Api;
  res = res + "context.Api.Name: "+api.Name+"\n";
  res = res + "context.Api.Path: "+api.Path+"\n";

  var url = context.Api.ServiceUrl;
  res = res + "context.Api.ServiceUrl.Scheme: "+url.Scheme+"\n";
  res = res + "context.Api.ServiceUrl.Host: "+url.Host+"\n";
  res = res + "context.Api.ServiceUrl.Port: "+url.Port+"\n";
  res = res + "context.Api.ServiceUrl.Path: "+url.Path+"\n";
  res = res + "context.Api.ServiceUrl.QueryString: "+url.QueryString+"\n";
  
  var dirs = context.Api.ServiceUrl.Query;
    foreach (var ent in dirs) {
         res = res + "context.Api.ServiceUrl.Query['"+ent.Key+"']: "+ent.Value[0]+"\n";
    }
  
  var req = context.Request;
  res = res + "context.Request.Method: "+req.Method+"\n";
  res = res + "context.Request.IpAddress: "+req.IpAddress+"\n";
  res = res + "context.Request.Method: "+req.Method+"\n";
  
    url = context.Request.Url;
  res = res + "context.Request.Url.Scheme: "+url.Scheme+"\n";
  res = res + "context.Request.Url.Host: "+url.Host+"\n";
  res = res + "context.Request.Url.Port: "+url.Port+"\n";
  res = res + "context.Request.Url.Path: "+url.Path+"\n";
  res = res + "context.Request.Url.QueryString: "+url.QueryString+"\n";

  dirs = context.Request.Url.Query;
    foreach (var ent in dirs) {
         res = res + "context.Request.Url.Query['"+ent.Key+"']: "+ent.Value[0]+"\n";
    }

    url = context.Request.OriginalUrl;
  res = res + "context.Request.OriginalUrl.Scheme: "+url.Scheme+"\n";
  res = res + "context.Request.OriginalUrl.Host: "+url.Host+"\n";
  res = res + "context.Request.OriginalUrl.Port: "+url.Port+"\n";
  res = res + "context.Request.OriginalUrl.Path: "+url.Path+"\n";
  res = res + "context.Request.OriginalUrl.QueryString: "+url.QueryString+"\n";

  dirs = context.Request.OriginalUrl.Query;
    foreach (var ent in dirs) {
         res = res + "context.Request.OriginalUrl.Query['"+ent.Key+"']: "+ent.Value[0]+"\n";
    }

  dirs = context.Request.Headers;
    foreach (var ent in dirs) {
         res = res + "context.Request.Headers['"+ent.Key+"']: "+ent.Value[0]+"\n";
    }
  
  var dir = context.Request.MatchedParameters;
    foreach (var ent in dir) {
         res = res + "context.Request.MatchedParameters['"+ent.Key+"']: "+ent.Value+"\n";
    }

    return res;  
}</set-body>
  </outbound>
  <on-error>
    <base />
  </on-error>
</policies>

4. Publish this service on the portal.

HTTP Verb: POST
URL Template: /customers/{cid}/orders/{oid}/?date={date}
Rewrite URL Template: /newLocation

5. Go to the developer portal and try this service operation with the following: http://fyi.azure-api.net/test/customers/101/orders/909/?date=today&status=new.

You will see the following output:

Cache-Control: private
X-Powered-By: ARR/3.0,ASP.NET
Content-Length: 1469
Content-Language: en
Content-Type: text/html; charset=utf-8

Request Received:
context.Api.Name: Test API
context.Api.Path: /test
context.Api.ServiceUrl.Scheme: http
context.Api.ServiceUrl.Host: echoapi.cloudapp.net
context.Api.ServiceUrl.Port: 80
context.Api.ServiceUrl.Path: /api
context.Api.ServiceUrl.QueryString: 
context.Request.Method: POST
context.Request.IpAddress: xx.xx.xx.xx
context.Request.Method: POST
context.Request.Url.Scheme: http
context.Request.Url.Host: echoapi.cloudapp.net
context.Request.Url.Port: 80
context.Request.Url.Path: /test/newLocation
context.Request.Url.QueryString: ?date=today&status=new
context.Request.Url.Query['date']: today
context.Request.Url.Query['status']: new
context.Request.OriginalUrl.Scheme: http
context.Request.OriginalUrl.Host: fyi.azure-api.net
context.Request.OriginalUrl.Port: 80
context.Request.OriginalUrl.Path: /test/customers/101/orders/909/
context.Request.OriginalUrl.QueryString: ?date=today&status=new
context.Request.OriginalUrl.Query['date']: today
context.Request.OriginalUrl.Query['status']: new
context.Request.Headers['Connection']: Keep-Alive
context.Request.Headers['Content-Length']: 281
context.Request.Headers['Content-Type']: text/xml
context.Request.Headers['Host']: fyi.azure-api.net
context.Request.MatchedParameters['CID']: 101
context.Request.MatchedParameters['OID']: 909
context.Request.MatchedParameters['DATE']: today

 

Using .NET CLR Types in Azure API Policy

'context.Request.Body' Request Body

Using Azure API Policy

⇑⇑ Microsoft Azure API Management Tutorials

2018-01-24, 13385🔥, 0💬