Mocking - replace url in responses (HATEOAS)

(Thomas De Groof) #1

Problem statement

Our API uses the HATEOAS principle for resource link (specifically the JSON:API specification).

This creates the need to dynamically replace the base URL when using Stoplight’s mocking feature.

Implemented solution

In order to achieve this behavior, I’ve replaced the base url in examples with a variable:


Then added a custom scenario as extra step to the mock server global rules:

The scenario looks like this:

This way when can set the URL in 1 place, making sure the variable is replaced in all mock example responses.


1/ Is this the correct way to do this? Is there an easier solution?
2/ Is there a variable to fetch the hostname from the incoming request, instead of having to hardcode it?


We observe that this does not always work!
1 out of 2 times the extra step is skipped, resulting in the variable not being replaced.
What goes wrong here?

Thanks in advance for your help!

(Me) #2

Hey Thomas, I’ve had this problem rattling around in my brain for a few days and I finally thought of a thing. Now, I am recommend we do it for Prism v3.0 so it won’t help you immediately, but if you have the time can you swing by and let us know what you think?

(Thomas De Groof) #3

Hi @philsturgeon,

Apart from the ongoing discussion on Github, did you have a chance to look at the problem with the current implementation?

(Thomas Pytleski) #4

Hi @thomas.degroof,

You are actually really close to the right solution, the reason why it works about 50% of the time is because scenarios run parallel. So what you really want to do, is create a new scenario, that has two steps, one is a ref to the After Mock Scenario and the other is a ref/script step for your url-replace scenario.

Then you just need one after rule, and reference the scenario you just created.

Here is the url for the After Mock Scenario -> When reffing it in a scenario, select External Link and paste, then select the After Mock Scenario as the target.

Let me know if you have any questions, or get stuck.

Good luck,


(Thomas De Groof) #5

Hi @bear!

Thanks for the explanation, that makes total sense now!
I got it working without any issues now.

Thanks again - if you need any helping beta testing the new Prism version or helping with docs, let me know :slight_smile: