Azure APIM - Policy - Set-Body - System.DateTime.UtcNow not retrieved

juni dev 336 Reputation points
2022-11-18T18:35:09.707+00:00

Hi

I'm using the Set-body policy to set some attributes
One of them needs to have he datetime of the operation

The operation runs well but the

  • UpdatedDateTime attribute always gets an empty string

. what is wrong on my C# expression?

    <set-body template="liquid">  
    {  
    "UpdatedBy": "{<!-- -->{context.Request.Headers["ocp-apim-subscription-key"]}}",  
    **"UpdatedDateTime": "{{System.DateTime.UtcNow.ToString()}}"**  
    {%if  body.displayName %} ,"displayName":"{{body.displayName}}" {% endif %}  
    }     
    </set-body>  

Many thanks,
JD

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,751 questions
0 comments No comments
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,141 Reputation points
    2022-11-18T21:37:47.063+00:00

    @juni dev Thank you for reaching out to Microsoft Q&A. Using liquid template, you can access request, response information along with context object. But you cannot evaluate policy expression like above. Alternatively, you can set policy expression value in variable, and then you can access it like below:

    <set-variable name="UpdatedDateTime" value="@(System.DateTime.UtcNow.ToString())" />  
            <set-body template="liquid">  
    			"UpdatedBy": "{{context.Request.Headers["ocp-apim-subscription-key"]}}",  
                "UpdatedDateTime": "{{context.Variables["UpdatedDateTime"]}}"  
                {%if  body.displayName %} ,"displayName":"{{body.displayName}}" {% endif %}  
    </set-body>  
    

    Feel free to add a comment if you have any other questions. We would be happy to assist you. Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community.


0 additional answers

Sort by: Most helpful