Beeceptor: 如何使用路径参数创建模拟规则

huangapple go评论52阅读模式
英文:

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

问题

我正在使用Beeceptor来模拟一个API。我可以访问一个硬编码的路径,但当我尝试将路径参数添加到模拟URL时,我收到一个响应,说这个路径没有配置。我尝试了以下方法:一个正则表达式作为路径参数:^[a-zA-Z0-9]$,一个转义后的正则表达式/^[a-zA-Z0-9]$/以及通配符*。我应该使用什么替代?

我注意到Beeceptor有一个用于URL的匹配规则称为"contains",但这不够好,因为你可以有仅在路径参数数量上不同的路径。

英文:

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.

答案1

得分: 1

匹配请求路径中的任何实体ID

Beeceptor支持JavaScript样式的正则表达式。如果要匹配路径参数,您不需要使用^$。考虑以下正则表达式规则,您应该使用它来匹配请求路径中的任何员工ID。这里[a-zA-Z0-9]*是一个直接的正则表达式。您可以使用[0-9]*\d*来仅匹配数字。

在以下模拟规则中要使用的正则表达式:/employee/[a-zA-Z0-9]*/salary

提取实体ID并在响应载荷中发送

您可以进一步使用命名的正则表达式组。这有助于您选择一个或多个路径参数,并在响应主体中使用它们。此功能称为动态模拟响应,其中使用句柄模板来构建所需的响应载荷。以下是一个示例,它将路径参数提取并在响应中使用为entity_id

Beeceptor: 如何使用路径参数创建模拟规则

英文:

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

Beeceptor: 如何使用路径参数创建模拟规则


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.

Beeceptor: 如何使用路径参数创建模拟规则

huangapple
  • 本文由 发表于 2023年5月24日 20:24:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定