Azure APIM - Return response - set-body on error construct a JSON response does not work
21:22 01 Feb 2026

Here is my policy statement. I need to construct and return a JSON response upon . I struggled a lot, tried using liquid template, string interpolation. Nothing worked. Help me.


    
        
        
        
        
        
    
    
    
        
    
    
    
        
    
    
    
        
        
        
            @(Convert.ToString(context.Variables["apimErrorContextMessage"]))

        
        
            
            
                application/json
            
                  @{ 
        var msg = Convert.ToString(context.Variables["apimErrorContextMessage"]) 
        return $@"{{
          ""status"": 500,
          ""message"": ""{msg}"",
          ""details"": ""{context.LastError?.Message}""
        }}";
      }
        
    

azure-api-management apim