Different Data Fields for Different Requests

I am new to Postman, and have a question about using data files for collections with multiple requests.

I saw:

Is It able to run difference requests with different data files in one Collection? - :person_raising_hand: Help - Postman Community
How to read data from two csv files for two different requests in same collection? - :person_raising_hand: Help - Postman Community

but neither fully answered my question.

Suppose I have a collection containing 2 GET requests, one of which requires values A and D, and the other which requires fields A, B, C, and D. How do I structure the data file in order to test the first request with a certain set of values, and the second request with a second set of values?

Like, if I have:

A B C D
1 1
2 2
3 3
1 10 95 1
2 20 85 5
4 30 75 6

How can I ensure that only the first three rows are used to test the first request and that only the last three rows are used to test the second request?

Hey @jeremy-bao :wave:

Welcome to the Postman Community! :postman:

What’s currently happening when you run the Collection now? Are you able to share the actual request and how you’re using those variables?

If you have no reference to the other fields in the datafile, within the requests, it’s not going to pick up those values.

I’m looking at this in a very basic sense and you may have other things going on in those requests or the Collection. If you’re not using the variable in the request, it’s not going to be used.

So here A would only be used in the First Request:

First Request:

{
    "property_1": "{{A}}",
    "property_2": "{{B}}",
    "property_3": "{{C}}"
}

Second Request:

{
    "property_2": "{{B}}",
    "property_3": "{{C}}",
    "property_4": "{{D}}"
}

I don’t have a collection that is using a data file currently. I am trying to figure out how to do that.

I don’t understand your example. What do you mean by “First Request” and “Second Request”? Are those objects within a JSON data file?

Like, let us say that the first type of request takes a body of the form:

{
    "A": ...,
    "B": ...
}

and the second takes requests of the form:

{
    "A": ...,
    "B": ...,
    "C": ...,
    "D": ...
}

How do you tell the data file to use some of its rows for the first request, and some of its rows for the second, and not to use the rows for the first request for the second request, and not to use the rows for the second request for the first request?

It was a crude mocked example to demonstrate how that would look with your question about using a set in the first request and a different request in the second.

The objects I used could be a simple request body, the {{X}} used in the values, is how you would reference the keys in your datafile which would correspond to the value in a given iteration.

More information about using datafiles with the Collection Runner:

I would recommend looking through that content to understand a little bit more about how to use datafile in the Collection Runner, it might close some of the knowledge gaps you have here.

I already read the first link, and I just watched the two youtube videos. None of them contain the information I need to solve my issue. They all deal with the case where you are just running a single request with multiple sets of data. I am trying to deal with the case where you are running several different requests, each using a different set of data.

It’s the same principle with adding data to the requests that are going to be used. Doesn’t really matter how many requests that you have.

The datafile just contains values, if you don’t reference any of the values in a request, then it’s not going to use that value during the run.

If you’re using a csv datafile then each line is a single iteration and if you’re using a json datafile then each object is a single iteration. The Collection Runner will run each of the requests in a single iteration and pick up the variables {{var_name}} that you have referenced in them. If you haven’t used a variable reference in a request, it won’t use it from the file.

Have you set up your requests and tried running them in the runner? Having a working example in front of you will be easier that just trying to figure it out with that :sweat_smile:

I created a data file and a request capable of using it. It ran on the rows of the CSV data file with relevant data, but also on those without it, and failed all the tests in those cases.

Like, I can’t share my data because it contains sensitive information, but here is some analogous stuff (where my request needs fields A and C):

A,B,C
1,,1
2,,2
3,,3
,4,4
,5,5
,6,6

My request should only use the first three rows of the data file. It does use those, and passes the tests in those cases. However, it also (incorrectly) uses the data from the last three rows, even though I left the A field blank in those rows.

@jeremy-bao leaving it blank will not cause it to omit that row because B and C have values in that row. Your best bet to omitting a row is not to include it at all. Remember that that value in each row will be stored as a variables A, B, C for each iteration of that request.

The collection runner will run your collection once for each iteration.

Therefore, it will always run both requests.

The newish Skip request feature might meet your use case.

Skip request execution | Postman Learning Center

You would need to include an extra column or key to denote which request it should be run against.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.