How can I throw exceptions in set-body in Azure API Management? - azure-api-management

The API that I'm working on now throwing 500 Internal Server Error when the request body do not have certain property inside. I want to eliminate that and instead, it returns another status code such as 400 Bad Request. I get that the error is return from APIM from Azure, how can I throw the error inside the set-body policy since I deserialize the request body in here?
I've read many posts and documents but no luck on succeeding that. Here's my code right now:
<inbound>
<send-request mode="new" response-variable-name="" timeout="60 sec">
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>#{
var request = context.Request.Body.As<JObject>(preserveContent: true);
if(String.IsNullOrEmpty((string)request["id"])){
WHAT SHOULD I PUT HERE?
}
return request.ToString();
}</set-body>
</send-request>
</inbound>
Thanks and appreciate if anyone could help.

You can use the return response policy in Azure API Management
https://learn.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#ReturnResponse

Related

Azure API Management - Authentication using HTTP query parameters

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

How to perform a 302 Redirect with a forwarding URL

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

Add different error message in Azure API Management validate_jwt policy

I have added the policy to validate jwt token in APIM. right now it validates and send error message and its all working fine. I am trying to send different error message for different scenario for e.g if the toke is not there or invalid I will return 401 with invalida token as error message or if the toekn is expired I want to send 401 with token is expired message. for this I am using something like below, is there any other way we can achieve instead have two different same policy?
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Invalid token or token not found." require-expiration-time="false" require-signed-tokens="true">
<openid-config url="server.config" />
<audiences>
<audience>serveraud</audience>
</audiences>
<issuers>
<issuer>serveriss</issuer>
</issuers>
</validate-jwt>
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Token Expired." require-expiration-time="true" require-signed-tokens="true">
<openid-config url="server.config" />
<audiences>
<audience>serveraud</audience>
</audiences>
<issuers>
<issuer>serveriss</issuer>
</issuers>
</validate-jwt>
You can put a condition check on the "failed-validation-error-message" in the policy. You may leverage "choose" condition :
https://www.codit.eu/blog/azure-api-management-conditional-policies/
APIM Policy to convert and incoming GET request to a POST request for my back end service

Azure API Management - Can I define a custom response status code?

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.

Mule - How to respond after receiving a request collection. (Mule Requester )

I am using the Mule Requestor to receive a collection of files via FTP.
In order to do this with a collection I am using the Collection Splitter which works great. The only problem is that when I am returning to the HTTP Connector it throws the following exception:
java.lang.UnsupportedOperationException: getPayloadAsBytes(), use
getPayload(DataType.BYTE_ARRAY_DATA_TYPE)
I am guessing this has something to do with the payload type that is trying to be inserted into the response, but I am fairly new in the Mule arena. Any idea what could be causing this and what I should do to resolve the issue?
I have tried setting the payload after the logger, that didn't work.
Code:
<flow name="fileGetter">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP" allowedMethods="GET"></http:listener>
<mulerequester:request-collection config-ref="Mule_Requester" resource="ftp_For_Requester_Dependent" doc:name="Mule Requester</mulerequester:request-collection>
<collection-splitter doc:name="Collection Splitter"></collection-splitter>
<logger level="INFO" doc:name="Logger" message="#[message.inboundProperties.originalFilename]"></logger>
</flow>
The reason you are getting java.lang.UnsupportedOperationException: getPayloadAsBytes(), use getPayload(DataType.BYTE_ARRAY_DATA_TYPE) is obvious, since you are splitting the payload, at the end your HTTP in causing the issue as there will be a split response .
One solution would be, use an aggregator at the end of your flow and that will prevent this exception.
A <collection-aggregator/> at the end will be a good choice and help to prevent this
UPDATE
Keep it under <async> block and it will run successfully