Iād like to match responses in a Postman mock server where the requests have a piece of variable data:
{
"data1": "constant",
"data2": "constant",
"timestamp": variable_timestamp
}
Is it possible to do this, or do POST bodies need to match exactly?
Hey @science-geologist-13
Welcome to the Postman Community!
You might be able to take advantage of the template helpers to return certain data from the request:
Hi Danny,
Thank you for the response. Itās not the return data thatās the issue for us. Itās that when we send variable POST data to the mock server, such as a timestamp, it doesnāt match any examples and we get a 404 back. Is there a way around this, or do we need to match POST data exactly?
Would you be able to provide an example of what youāre sending, what the example is and what the response is please?
Is it a dynamic variable or a static value stored in a variable scope?
Are the timestamps in the examples dynamic values or just static values. Do you have certain timestamps set in the examples for returning different data sets?
Just trying to build up more of a picture about the setup as I canāt see what you have in front of you.
Out service might send something like:
{
āidā: 1234,
āclassā: āoneā
ātimestampā: 1728753358
}
and then another request:
{
āidā: 1234,
āclassā: āoneā
ātimestampā: 1730000000
}
What we would like is for the mock server to ignore the timestamp and match on url/id/class.
Wouldnāt those templates in the example response body cover that?
{
"id": {{$body 'id'}},
"class": "{{$body 'class'}}"
"timestamp": {{$body 'timestamp'}}
}
It would take the payload youāre sending to the mock server and return that.
Thereās also certain headers that you can use to directly go to a specific request by name, Id or response code.
I donāt think this works with āResponse matchingā ā āRequest bodyā turned on. It seems like when I turn this on, thereās no way to have the mock server respond to anything but exactly defined sets of variables. For instance, if I send any timestamp that isnāt explicitly set in an example, the mock server will respond with a 404.
This gets at the heart of what Iām trying to figure out. Is there a way to have request body matching turned on and still send data that doesnāt exactly match examples?
Does that happen with this header in the request:
x-mock-match-request-body:false
Again, I canāt see what you have in front of you.
Images work better than trying to explain it with words.
We have this setting turned on because we need to match a variety of requests to the same URL:
Here is our request/example setup:
This works and returns a 200:
curl -i -X POST https://XXXXXX.mock.pstmn.io/data1 --header āContent-Type: application/jsonā -d ā{ātimeā: 1728682300, āotherā: 1111}ā
This doesnāt because the time no longer matches. This returns a 404:
curl -i -X POST https://XXXXXX.mock.pstmn.io/data1 --header āContent-Type: application/jsonā -d ā{ātimeā: 1728682311, āotherā: 1111}ā
I tried putting a wildcard in the example, but that didnāt allow the time to be flexible. I still received a 404:
In my contrived example, Iād like to be able to submit requests that always have āother: 1111ā, but have variable times. Is this possible?
Thank you for your help so far.
A wildcard would look like this:
{
"other": "1111",
"time": {{$body 'time'}}
}
This would be just reading the value from the request body property ātimeā and returning that value. That would always match what youāre sending.
You just have a variable placeholder in the example, from looking at your image.
This isnāt working for me. What am I missing?
1 Like
There are definitely more demonstrations and documentation of the Postman Mock Servers that are needed as even I feel like Iām learning something new each time
Weāll get there eventually and if itās still not working, Iāll reach out to other members of the team.
Did you also try that request with this header?
--header 'x-mock-match-request-body: false'
The basic request in my Collection:
I only have 1 wildcard example under my request which basically repeats whatās been sent in the requests body:
Running it in a CLI:
Iām sure the header would allow the request to match, but we need to match the body to differentiate between requests.
I guess what weāre looking for is partial body matching which sounds like it isnāt currently available. Weāll find a workaround for now.
I appreciate the support!
1 Like