How to match variable POST data

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 :wave:

Welcome to the Postman Community! :postman:

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. :pray:

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. :pray:

We have this setting turned on because we need to match a variety of requests to the same URL:
Screenshot 2024-10-15 at 5.37.37 PM

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:
Screenshot 2024-10-15 at 5.40.33 PM

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 :frowning:

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