Simple Mocking with Javascript Callout

It is possible to write some simple mocks using a Javascript callout, however if you have conditional logic or many responses, it would be better to use a library like apimocker in Hosted Targets.

Here is an example of a JavaScript policy that you could attach to the ProxyEndpoint Response PreFlow in an API Proxy with no target.

var verb = context.getVariable("request.verb");
var path = context.getVariable("proxy.pathsuffix")

if(verb.toLowerCase() === "get" && path.toLowerCase() === "/dogs") {
  context.setVariable("response.content", JSON.stringify({
  dogs: [
    {
      name: "fluffy"
    }
  ]
}))
}
Version history
Last update:
‎03-29-2020 12:17 PM
Updated by: