Kumar, Manish Based on your description of the use case, you need to extract the header or body using Context variable and then check for conditions after parsing it to System.DateTime
object in your policy. Refer similar thread: https://learn.microsoft.com/en-us/answers/questions/1189885/how-to-check-whether-the-current-date-time-falls-w which has policy reference and supported types.
For header input like this, try the below policy snippet:
Sample request header:
Policy Snippet:
<set-variable name="isDateWithinRange" value="@{
var createdDate = Convert.ToDateTime((String)context.Request.Headers.GetValueOrDefault("CreatedDate", ""));
var expiresDate = Convert.ToDateTime((String)context.Request.Headers.GetValueOrDefault("ExpiresDate", ""));
string isDateWithinRange = "No";
if(DateTime.UtcNow >= createdDate && DateTime.UtcNow <= expiresDate)
{
isDateWithinRange = "Yes";
}
return isDateWithinRange;
}" />
For XML request body, use XDocument.Parse()
method and we have a sample policy available here: Extracting multiple values from xml documents.policy.
Sample request body:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<CreatedDate>2023-03-17T08:53:17.866+00:00</CreatedDate>
<ExpiresDate>2023-03-18T08:53:17.866+00:00</ExpiresDate>
</Envelope>
Policy Snippet:
<set-variable name="isDateWithinRange" value="@{
var createdDate = Convert.ToDateTime((String)context.Request.Headers.GetValueOrDefault("CreatedDate", ""));
var expiresDate = Convert.ToDateTime((String)context.Request.Headers.GetValueOrDefault("ExpiresDate", ""));
string isDateWithinRange = "No";
if(DateTime.UtcNow >= createdDate && DateTime.UtcNow <= expiresDate)
{
isDateWithinRange = "Yes";
}
return isDateWithinRange;
}" />
Note, the above policy snippet is just for reference, and you can customize it based on your need. I hope this helps and let me know if any questions.
If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.