Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure API Management - Dynamic Mock Response

I am setting up a suite of Enterprise APIs but I want to be able to provide the consumers some "stub" endpoints to allow them to continue their development processes without the need for me.

So, as we are using Azure API Manager, I will be using MOCK responses to requests.

Now, I know that the any response will be a strictly defined data contract / schema but what I want to be able to do is to provide a different response payload content depending on the inbound request.

When setting up the Mock responses, this is done based on the HTTP Status Code and it appears that you can only set up one mocked response for each status, so you have one for 200 OK, 202 Accepted, and another for 500 Server Fault and so on.

Is there a way to say that for, say the 200 response, to have the option to reply with different content based on the request?

e.g.

As said, "Some Payload for Option ?" will conform to the agreed API contract but option A may have just 1 element in an array, but option B may have 5 elements.

/api/request?option=A => 200 OK {Some payload for option A}

/api/request?option=B => 200 OK {Some payload for option B}

like image 456
Chris Hammond Avatar asked Apr 19 '26 12:04

Chris Hammond


1 Answers

You can make use of APIM's transformation policies in the outbound policies to set a custom body based on your URL parameters.

       <when condition="@(context.Request.MatchedParameters["option"] =='A')">
<set-body>Some body for A</set-body>
         </when>
       <when condition="@(context.Request.MatchedParameters["option"] =='B')">
<set-body>Some body for B</set-body>
         </when>

PS: Not tested

like image 174
Justin Mathew Avatar answered Apr 21 '26 01:04

Justin Mathew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!