I’m trying to test backend connectivity in Azure API Management (APIM) using a send-request policy. I am trying in an automated solution for our developers to use an azuredevops pipeline where they can input the backend endpoint url to test the connectivity. I want to dynamically set the URL using a Named Value stored in APIM. We don’t have any way to test the network connectivity in the apim subnet as it’s a dedicated subnet and we would need this solution to check the connectivities enabled before creating the actual APIs.
However, I keep getting the error:
Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists.
I used the following policy:
<!-- Send request to the backend URL -->
<send-request mode="new" response-variable-name="connectivityTest">
<set-url>@(context.Variables["backendUrl"])</set-url>
<set-method>GET</set-method>
</send-request>
<!-- Check response and return result -->
<choose>
<when condition="@(context.Variables["connectivityTest"].StatusCode == 200)">
<return-response>
<set-status code="200" reason="OK" />
<set-body>Backend is reachable</set-body>
</return-response>
</when>
<otherwise>
<return-response>
<set-status code="502" reason="Bad Gateway" />
<set-body>Backend is not reachable</set-body>
</return-response>
</otherwise>
</choose>
What I Have Tried: • I tried using .ToString() but still received the same error. • I verified that the Named Value testBackendUrl exists in APIM and has a valid string value. • I also tried using different ways of casting but no luck.
How do I correctly convert the Named Value to a string to use in the element? • Is there a better way to reference Named Values in APIM policies?
Environment: • Azure API Management • Named Value testBackendUrl is defined in APIM with a placeholder value like .
Any help would be greatly appreciated!
Feel free to adjust the wording as needed before posting on Stack Overflow.
I’m trying to test backend connectivity in Azure API Management (APIM) using a send-request policy. I am trying in an automated solution for our developers to use an azuredevops pipeline where they can input the backend endpoint url to test the connectivity. I want to dynamically set the URL using a Named Value stored in APIM. We don’t have any way to test the network connectivity in the apim subnet as it’s a dedicated subnet and we would need this solution to check the connectivities enabled before creating the actual APIs.
However, I keep getting the error:
Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists.
I used the following policy:
<!-- Send request to the backend URL -->
<send-request mode="new" response-variable-name="connectivityTest">
<set-url>@(context.Variables["backendUrl"])</set-url>
<set-method>GET</set-method>
</send-request>
<!-- Check response and return result -->
<choose>
<when condition="@(context.Variables["connectivityTest"].StatusCode == 200)">
<return-response>
<set-status code="200" reason="OK" />
<set-body>Backend is reachable</set-body>
</return-response>
</when>
<otherwise>
<return-response>
<set-status code="502" reason="Bad Gateway" />
<set-body>Backend is not reachable</set-body>
</return-response>
</otherwise>
</choose>
What I Have Tried: • I tried using .ToString() but still received the same error. • I verified that the Named Value testBackendUrl exists in APIM and has a valid string value. • I also tried using different ways of casting but no luck.
How do I correctly convert the Named Value to a string to use in the element? • Is there a better way to reference Named Values in APIM policies?
Environment: • Azure API Management • Named Value testBackendUrl is defined in APIM with a placeholder value like https://dummy.url.
Any help would be greatly appreciated!
Feel free to adjust the wording as needed before posting on Stack Overflow.
Share Improve this question asked Feb 16 at 17:32 VowneeeVowneee 1,4791 gold badge24 silver badges68 bronze badges1 Answer
Reset to default 0I was able to get syntax accepted with
- changing from
context.Variables[]
to the safercontext.Variables.GetValueOrDefault<string>()
- wrapping
context.Variables["connectivityTest"]
withIResponse
<!-- Send request to the backend URL -->
<send-request mode="new" response-variable-name="connectivityTest">
<set-url>@(context.Variables.GetValueOrDefault<string>("backendUrl",""))</set-url>
<set-method>GET</set-method>
</send-request>
<!-- Check response and return result -->
<choose>
<when condition="@(context.Variables.GetValueOrDefault<IResponse>("connectivityTest",null).StatusCode == 200)">
<return-response>
<set-status code="200" reason="OK" />
<set-body>Backend is reachable</set-body>
</return-response>
</when>
<otherwise>
<return-response>
<set-status code="502" reason="Bad Gateway" />
<set-body>Backend is not reachable</set-body>
</return-response>
</otherwise>
</choose>
I did not run/test the code, but it reflects what we use in many other parts of our APIM code base.