Skip to content

Url Format

Ploiu edited this page Dec 4, 2024 · 3 revisions

When creating a route, there are a few rules the url must follow:

  • it must start with a / character
  • it must be a valid url path

Pretty simple, right? This is meant to be a pretty loose set of requirements so that you can set it to be whatever you want. There is a problem though: what if you want to mock a single endpoint that could take variable parts of the url? What if you have versioned endpoints and don't want to write a new matcher for each version? To support this, you can use path and query variables in your url.

Path and Query Variable Syntax

Path and query variables are preceded with the colon (:) character, followed by the variable name. These variables can be named anything so long as they don't have a character that could break the url (such as /, ?, or &). Some examples of urls with these variables:

  • /test/:name/:age
  • /test?:name&:age
  • /test/:name?:age

Optional parameters are accepted as well, and to mark a variable as optional, simply add ? to the end of the variable name. Here are some examples:

  • /test/:name?/:age
  • /test?:name?&:age
  • /test/:name??:age

With query params, you can also let the mock endpoint accept anything:

  • /test?:*
  • /test?:variable&:* They also can be pulled out into variables as well to be referenced in the response body, with {{double curly syntax}}

As you can see, optional and mandatory variables can be mixed. In the event a mock endpoint is hit without including optional variables, the variables will be filled in on a first come first serve basis. E.G.:

/test/:name?/:age/:favoriteColor?

can be hit with /test/23/blue, in which case name will be filled with 23 and age will be filled with blue. It can also be hit with just /test/23, and it will only fill in age as 23.

Clone this wiki locally