Second Set variable policy in Azure APIM throws error.

rahul b 20 Reputation points
2023-09-22T15:28:35.77+00:00
Hi everyone, From the send request policy response payload , I am trying to use the two set variable policy to get two different variables and set it in here in the policy. So for the first set variable policy I am able to extract it but not the second one azure is throwing exception.

"Expression evaluation failed. Object reference not set to an instance of an object.", 

        "Object reference not set to an instance of an object."

Please help me here. 
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,066 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,161 questions
{count} votes

Accepted answer
  1. Thomas Meads 1,586 Reputation points
    2023-09-24T11:20:29.95+00:00

    When accessing the Body element through context.Request.Body or context.Response.Body by default the body is cleared from the object. In your case it seems that you are accessing this through a variable however I suspect this is still the issue you are experiencing.

    To fix this simply add preserveContent: true to your .As<> method:

    <set-variable name="name1" value="@((String)((IResponse)context.Variables["response-variable-name"]).Body.As<JObject>(preserveContent: true)["result"][0]["Key1"])" />
    
    <set-variable name="name2" value="@((String)((IResponse)context.Variables["response-variable-name"]).Body.As<JObject>(preserveContent: true)["result"][0]["Key2"])" />
    

    For more info please see the notes on this article: https://learn.microsoft.com/en-us/azure/api-management/set-body-policy

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.