locked
Sending JSON inside policy definition RRS feed

  • Question

  • Hi,

    I would like to send POST request with JSON. How can I do that ?

    This is a sample policy by using POST form:

    <policies>
        <inbound>
      <!-- Extract Token from Authorization header parameter -->
      <set-variable name="token" value="@(context.Request.Headers.GetValueOrDefault("Authorization","JWT").Split(' ').Last())" />
    
      <!-- Send request to Token Server to validate token (see RFC 7662) -->
      <send-request mode="new" response-variable-name="tokenstate" timeout="20" ignore-error="false">
        <set-url>http://serverauth/auth/authorization</set-url>
        <set-method>POST</set-method>
        <set-header name="Content-Type" exists-action="override">
          <value>application/x-www-form-urlencoded</value>
        </set-header>
        <set-body>@($"token={(string)context.Variables["token"]}")</set-body>
      </send-request>
      
      <choose>
        <when condition="@((bool)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["active"] == false)">
          <return-response response-variable-name="existing response variable">
          <set-status code="401" reason="Unauthorized" />
          </return-response>
    
        </when>
    </choose>
      <base />
    
    </inbound>
    </policies>


    Adityo Setyonugroho

    Monday, September 18, 2017 8:17 AM

Answers

  • Refer below link which uses send request to send a JSON body.
    https://docs.microsoft.com/en-us/azure/api-management/api-management-sample-send-request

            <set-method>POST</set-method>
            <set-body>@{
                    return new JObject(
                            new JProperty("username","APIM Alert"),
                            new JProperty("icon_emoji", ":ghost:"),
                            new JProperty("text", String.Format("{0} {1}\nHost: {2}\n{3} {4}\n User: {5}",
                                                    context.Request.Method,
                                                    context.Request.Url.Path + context.Request.Url.QueryString,
                                                    context.Request.Url.Host,
                                                    context.Response.StatusCode,
                                                    context.Response.StatusReason,
                                                    context.User.Email
                                                    ))
                            ).ToString();
                }
    </set-body>
    
    

    ------------------------------------------------------------------------------------------------------------------

    Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members.


    Thursday, September 21, 2017 3:01 AM