how do I use set-header with a datetime variable

Martin Kallukalam 355 Reputation points
2024-08-07T23:42:17.0133333+00:00

    <inbound>

        <base />

        <set-variable name="starttime" value="@(DateTime.UtcNow)" />

        <set-header name="Policy-Execution-TimeStamp" exists-action="append">

            <value>@(starttime.ToString("r"))</value>

        </set-header>

    </inbound>

I get error "The name 'starttime' does not exist in the current context"
what is the correct syntax?

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

Accepted answer
  1. JananiRamesh-MSFT 27,751 Reputation points
    2024-08-08T05:10:08.3033333+00:00

    @Martin Kallukalam Thanks for reaching out please refer the below policy snippet

    <inbound>
            <set-variable name="starttime" value="@(DateTime.UtcNow.ToString("r"))" />
            <set-header name="Policy-Execution-TimeStamp" exists-action="append">
                <value>@((string)context.Variables["starttime"])</value>
            </set-header>
        </inbound>
    
    
    

    hope this helps! do let me know incase of further queries, I would be happy to assist you.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Martin Kallukalam 355 Reputation points
    2024-08-10T05:16:11.3066667+00:00

    duplicate of above comment.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.