I am trying to do something like this:
<set-backend-service base-url="https://{{context.Variables['instance']}}/api" />
Or
<set-backend-service backend-id="my-{{context.Variables['instance']}}-api"/>
But getting errors that I've given an invalid URL or that APIM can't locate that backend. Is there a way to parameterize your backend?
The way you are trying is only applicable in liquid templates when using the set-body policy OR when using Named Values. In other scenarios, it has to be a policy expression.
Something like this can be done instead
<set-variable name="resource-group-name" value="rg-01" />
<set-variable name="logic-app-name" value="la-01" />
<set-backend-service id="apim-generated-policy" backend-id="#("LogicApp_" + context.Variables["resource-group-name"] + "_" + context.Variables["logic-app-name"])" />
PS: The example above is for a Logic App backend created when importing one from the Portal UI.
Related
I want to expose an API that uses HTTP query parameters for authentication. Something like this:
{root-backendURL}/resource?login=loginName&password=loginPassword
I am using Azure API Management. I've created a new API which points to the backend service, and I've created a new operation called "Resource". For that operation I've added two Query parameters that are required.
Now I want to expose and make use of the APIM Gateway URL that should look something like this:
{root-GatewayURL}/apiName/resource
Adding those two required query parameters will make the URL look like this:
{root-GatewayURL}/apiName/resource?login=loginName&password=loginPassword
When I test the Gateway URL using Postman, I get a 401 response.
{
"code": 401,
"errors": [
{
"message": "Authentication failed."
}
]
}
I've tried to use a Send-Request policy and add from there the query parameters. This way it works.
<rewrite-uri template="/" />
<set-variable name="login" value="loginName" />
<set-variable name="password" value="loginPassword" />
<send-request mode="new" response-variable-name="resource" ignore-error="false">
<set-url>#($"{root-BackendURL}/resource?login={(string)context.Variables["login"]}&password={(string)context.Variables["password"]}")</set-url>
<set-method>GET</set-method>
</send-request>
<return-response>
<set-body>#(new JObject(new JProperty("resource",((IResponse)context.Variables["resource"]).Body.As<JObject>())).ToString())</set-body>
</return-response>
I've double checked the URLs and they are identical. Why do I get different results?
I do not want to use the policy, I want to make it work using the query parameters, because I need to use also an ID for the resource and also other query parameters, for example:
{root}/resource/{id}?limit=25&offset=50&loginName&password=loginPassword
Is it possible to populate the Web Service URL based off some parameter / variable that gets sent to the Base URL?
I want to be able to forward different inbound requests to API Management to different target servers, without having to create new APIs for each new server.
You can specify different backends with a policy.
There you can add your custom logic for different query values, params, headers,...
<policies>
<inbound>
<choose>
<when condition="#(context.Request.Url.Query.GetValueOrDefault("version") == "2013-05")">
<set-backend-service base-url="http://contoso.com/api/8.2/" />
</when>
<when condition="#(context.Request.Url.Query.GetValueOrDefault("version") == "2014-03")">
<set-backend-service base-url="http://contoso.com/api/9.1/" />
</when>
</choose>
<base />
</inbound>
<outbound>
<base />
</outbound>
</policies>
API Management transformation policies
On Azure APIM I have an operation that has querystring parameters.
In the policy I change the backend url and apply a rewrite-uri to alter the url.
Only the querystring from the caller is not applied to the backend.
Is it possible to passthrough the incoming querystring directly to the backend without changing anything?
<inbound>
<base />
<set-backend-service base-url="https://api.test.eu" />
<rewrite-uri template="dispatch/gateway/test" />
</inbound>
UPDATE
Fixed by using the folloing:
<set-backend-service base-url="https://api.test.eu" />
<rewrite-uri template="/dispatch/gateway/test" copy-unmatched-params="true" />
We are using Xamarin and UITest in order to create UI tests for our application.
We are using Azure API management for integration, and am trying to mock our Authentication process.
We are using Xamarin Essentials WebAuthenticator, which opens an external browser in order to do the two step OAuth and load a login page.
Since UITest cannot control anything outside of the app itself, we need to have API management respond by giving a 302 redirect back to the controlling app, with our test authentication tokens that are mocked.
I've tried creating a mock response that returns a 302, with a Location header with the value of our return url for the app. However whenever I test this in the APIM test page, it simply just returns a 200 OK.
Is it possible to make a GET request to APIM, which performs a 302 redirect to a specified uri, with a body of mocked test data?
You may try with a return-response policy.
Something similar to this example:
<when condition="#(context.Request.OriginalUrl.Scheme == Uri.UriSchemeHttp)">
<return-response>
<set-status code="302" reason="Temporary Redirect" />
<set-header name="Location" exists-action="override">
<value>#{
var uri = context.Request.OriginalUrl;
return Uri.UriSchemeHttps + "://" + uri.Host + uri.Path + uri.QueryString;
}</value>
</set-header>
</return-response>
</when>
For your reference: https://learn.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#ReturnResponse
When setting up the all the possible response codes and their descriptions for an endpoint in API Management Service, I can only choose an existing status code from a dropdown list, but it doesn't allow me to add a custom status code, such as 499. I use the 499 status code in my API for a specific type of error relevant to my applicatoin. Is there any way to add this to the list of responses?
You could dynamic mocking using APIM with an APIM policy called return-response.
<return-response>
<set-status code="200" reason="OK" />
<set-header name="content-type" exists-action="override">
<value>"application/json"</value>
</set-header>
<set-body>{
"id": "cat12345",
"name": "Garfield",
"tag": "Sleepy Cat"
}</set-body>
</return-response>
For more details, you could refer to this tutorial.
Not with forms UI. But if you select an API, there'll be a pencil in Frontend block on Design tab. It allows you to edit your API specification in OpenAPI 2/3 spec. Using that it should be possible to add a custom response statuses code.