restmockingmockserver

Beeceptor: How can I have a mock rule with path param


I am using beeceptor for mocking an API. I can access a hard-coded path, but when I try to add path params to the mock URL I get a response saying that nothing is configured for this path. I tried the following: a regexp as a path param: ^[a-zA-Z0-9]$, an escaped regexp /^[a-zA-Z0-9]$/ and a wildcard *. What should I use instead?

I see that beeceptor has a match rule for URL called contains but it's no good because you can have paths that only differ in the number of path params.


Solution

  • Matching a request path with any entity id

    Beeceptor supports Javascript style regular expressions. You do not need to use ^ or $ if you want to match path parameters. Consider the following regex rule that you should use to match any employee-ID in the request path. Here [a-zA-Z0-9]* is a straight forward regular expression. You can use [0-9]* or \d* to match only numbers.

    RegEx to be used in the following mocking rule: /employee/[a-zA-Z0-9]*/salary

    request-path-parameter-regex


    Extract entity id and send in response payload

    You can go further and use named regex groups. This helps you pick one or more path-parameters and use them in the response body. This feature is called Dynamic Mock Responses, where handlebar templates are used to build desired response payload. Here is an example which is extracting the path-param and using in the response as entity_id.

    enter image description here