Mock server and regular expression

Can we use in example request regular expression?

currently, i have:

{
    "timestamp": 123456,
    "customerid": 1212,
    "Token": "sdcgsdfhgdfsjnhasdfgvweDSTG245TRWEFG"
} 

can I code something like that?

{
    "timestamp": [0-9]{6},
    "customerid": 1212,
    "Token": "sdcgsdfhgdfsjnhasdfgvweDSTG245TRWEFG"
}

Hey @peri.gold81

Welcome to the community! :wave:

Could I ask what the outcome is there you’re seeking?

Do you need to verify a certain field in the response to check if it’s a certain format or do you just need some dynamic data placed into the field on the example request?

i want to catch dynamic request that answer to specific pattern

as example:

{
    "timestamp": [0-9]{6},
    "customerid": [0-9]{4},
    "Token": "sdcgsdfhgdfsjnhasdfgvweDSTG245TRWEFG"
}

i want to catch number with 6 digits

the response will be dynamic too…

{
    "timestamp": same as request,
    "customerid": same as request,
    "permission": "login granted"
}

Sorry I still don’t really understand what you’re trying to achieve, if you could elaborate more and explain the use case in more detail, it might help me and others to understand the problem.

A couple of points on the examples for Mock servers - You currently cannot use regex in the example request body, as you have it but you can add a layer of dynamic data to them.

The built-in Dynamic variables can be used in the examples to create random fake data - Here’s a basic request body:

{
    "timestamp": {{$timestamp}},
    "customerid": {{$randomInt}},
    "Token": "{{$guid}}"
}

That would return a response body in the mock server request like this:

{
    "timestamp": 1583748625,
    "customerid": 883,
    "Token": "0bc71255-159c-41e4-83ef-7bd602897acf"
}

You can find a number of different random variables that can be used here.

1 Like

what i want…

i want to send request with customerid, the number will be 4 digits(1234,3234,7891)
i want the example to take this number lets take 1234 as example and catch it with postman mock example

because i want the example to check the number of digits{4} and not the value 1234

i want → “customerid” : [0-9]{4}"
not “customerid:1234”

next day i want to send 3434 and the same example will use…

the customer will not be 1234 every time… it will be 4 digits and that should be ok…

BTW thanks for your time to response me.

i’m checking postman for my company see if the mock server can help us in our projects…

currently its postman or mocklab site :slight_smile:

You can do something like this via the URL, create your Mock example like this with an unresolved variable:

When you send a value in the path of the URL /user/1234 it will use that value in the example response.

So you would be able to use the same example for multiple requests.

1 Like

really thanks for your answer… but again…

I want to use regular expression in the BODY of the REQUEST of the EXAMPLE

you use the parameter in the url.

i want to send
request:

{{url}}/login

{
    "customerid":1234
}

in the example:
request

{{url}}/login

{
    "customerid":[0-9]{4}
}

example response

{
    "customerid":[0-9]{4}
    "permission":"login granted"
}

I did mention that you couldn’t do that and I was offering a different solution.

I understand what you want to send in the body and what you would like in the example but that’s not possible.

There are ways to match the request body but using a specific header but again, that’s not going to give you the regular expression syntax that you want to see/use in an example.

I think the closest you could get to what you require is to create a request that contains the x-mock-match-request-body header with the value true and then create a series of examples with those different request/response bodies.

The example would need to match for the method, URL and request body and then you can add whatever you need to the response body.

When sending requests with different values in the Request Body, it would respond with one of the examples:

ok thanks… so this tools implemented what i need…

because i dont know which customerid the API will send, i just know it will send post and inside the body in json structure the customerid will be numeric with 4 digits (1111,3245,6532…)

i cant create example request with constant number (1234)… and i can send it with the url because its an other request

request:
/url/login

{
“customerid”:1234…
}

not same request as:

/url/login?customerid=1234
{
…
}

thanks for your answrs

Thanks, this was exactly what I was looking for! The variable in the example response doesn’t get populated if you use the :var syntax. I really think this should be included in the official documentation, as I have spent more than an hour with Google and trial-and-error on this. It feels like a common use case for mock servers, to include a path or query variable in the mocked response.

2 Likes

Now I would also like to include fields from the request body in the response body. So, I would like to define my Example request body as something similar to (JSON):

{
    "name": "{{name}}",
    ...
}

And get the Example to respond with:

{
    "name": "{{name}}",
    ...
}

I.e. that the “name” field’s value sent in the request gets copied to the “name” field in the response. Is this possible?

For anyone else reading this thread, copying fields from request to mocked response is currently not possible. A Github issue has been created requesting this feature:

1 Like

hello,
i’ve found this post searching for a feature like this,

Is this something planned ?, is this something already feasible with Postman ?
the use case we have is mocking dynamic graphql request,
from my understanding they cannot be predicted unless some kind of pattern matching algorithm for the request body is possible.

You can create multi examples , name them like

Value1-response
Value2-response

Etc,

And you can call that specific response to be returned by mock server by using

X-mock-response-name: value1-response 

In the header .

If you can save a response value from a request test, as the link below describes, I wonder if you’d be able to save a request value as well, and then use it?