Body file variable substitution

I’m an old programmer, ask me about COBOL sometime!
But I’m new to postman.

Is there a way to get postman to do variable substitution(s) when reading the request body from an external file?
I may be missing something very simple or painfully obvious, so any help would be appreciated.

Details are as follows:

I have a post request that uses two variables defined in the environment:

  1. the post URL uses the variable {{theURL}} to send the request to one of many servers
  2. the body uses the other variable {{myIP}} to forward the application GUI back to my PC

When I included the body in the request as a (raw) and the request functions as I expect.
The data is sent to the server specified by my environment variable {{theURL}}
The application forwards the GUI replies back to my PC using the enviroment variable {{myIP}}
The world is good.

I take the body text and put it into an external file, lets call it fred.json
I change the postman body to (binary), press select file, and select fred.json.
When I run this request thru postman it goes to the server specified by {{theURL}}
The application runs, but the GUI is not forwarded to my PC.

I edit fred.json and replace the variable {{myIP}} with the environment value, and save the file.
I run this request thru postman again.
It is sent to the correct server specified by {{theURL}}
The application runs and the GUI is now forwarded to my PC.

It looks to me like the external file is not being processed for any variable substitutions.

Thanks

Hi @dmhunter,

I don’t believe variable substitution happens on file-based request bodies.

Is it possible to include the contents of the file as a raw body in Postman with a JSON type? Variable substitution will happen with that approach.

Best,

Kevin

Kevin,

That was how I determined that the variable substitution actually worked in the first place.
I described that process in the first part of my posting.

I agree that including the body text inside of the postman body (raw-JSON) does work.
And that isn’t too much of a problem with a list of static tests.
Or even with a small list of dynamic tests - can you believe it, but users actually change their minds about what they want and applications do change over time.

My situation is the complete opposite.
A large and constantly changing list of requests.
When I say large I mean hundreds or thousands of requests.
When I say changing I mean monthly, weekly, daily, or hourly changes to sections of the application I am testing with these requests.
I think we can both agree that manually modifying that request list is not really an option.

That is why I decided to use external files containing the JSON request.
It is also why I am asking if there is a way to have the external file read and then processed as if it were text included in the postman request.

Thanks for your reply Kevin.

Dan

Hey @dmhunter,

There are a lot of different ways to solve this depending on your scenario and constraints.

  1. Do you have a few requests with a large number of request bodies (from JSON files) to iterate through?
  2. Do you have a large number of requests, each with unique corresponding request bodies?
  3. Do you need to run this through the Postman GUI app or could this be executed via a CLI?

With such a great volume and a variable rate of change, it may make more sense to script this with Newman either via shell scripting with the CLI or as a library with Node.js.

If using the Node.js library approach, you can load each file into memory, do the variable substitution using any template engine you like, and execute collection runs. Another approach is to still use the file system. You may be able to plug into a beforeRequest event and handle variable substitution. Additional reference: More about file uploads and collections with Newman.

If using a shell scripting approach, you may opt to do variable substitution before running Newman. I can recommend Jsonnet for complex workflows. Otherwise, sed can often suffice.

Best of luck,

Kevin

@dmhunter I am having a hard time understanding what you are trying to do. It would help a lot of you could provide some screenshots to get a better idea.

We have a custom application that has some data passed to it thru the JSON body.
The key line is “testWatchGui” : “192.168.1.2:0”

Case #1)
If I hard code that line as above in the RAW-JSON body of the postman test, it works.
If I hard code that line as above in the JSON file, select it in the body of the postman test, it works.
In the http dispatch log I see two instances of the GUI forwarding to 192.168.1.2
So a hard coded IP address works with our application.

Case #2)
This time the line says “testWatchGui” : “{{myIP}}”
In my environment I define the variable myIP as 10.1.155.8
If I hard code that line as above in the RAW-JSON body of the postman test, it works.
Looking at the http dispatch log I see the GUI being forwarded to 10.1.155.8
If I hard code that line as above in the JSON file, select it in the body of the postman test, it fails.
Looking at the http dispatch log I see the GUI being forwarded to {{myIP}}
So there was >>NO<< variable substitution done when reading the JSON file.

This is childs play to duplicate.
Create a new collection, call it fred.
Take a short fast running request of your own.
Copy it to a new collection with a name of Run.1
Extract the body logic and save it to a new file, call it RUN2.json.
Duplicate Run.1 and rename it Run.2.
Delete the body information, press the “Select” button and select RUN2.json.
Duplicate RUN.1 and rename it Run.3
Change a single piece of data to a new variable, call it “{{BARK}}”.
Add the variable “BARK” to the environment and give it the correct value.
Extract the body logic and save it to a new file, call it RUN4.json.
Duplicate Run.3 and rename it Run.4.
Delete the body information, press the “Select” button and select RUN4.json.

Save this collection.

Run this collection.
Run.1 will complete normally.
Run.2 will complete normally.
Run.3 will complete normally.
Run.4 will fail and generate errors.

If you look at your logs after each run, you should be able to see that the last run tried to do something with “{{BARK}}”, while all of the previous runs did things with the value of the environment variable “BARK”.

This ain’t rocket science.

I will see if I can find an http site that accepts a post an create the collection I described above.
If I can, I will post the exported collection, the exported environment, Run.1, and Run.2.

OK, I did it.
I used the site http://httpbin.org/post - NOTE: Wonderfully useful!!!
Unfortunately I can’t post the exported collections.
Here are the images that I can upload.
Limited to 3 image. &$(@&(@$!!!

This can’t be made any simpler.

File-Run.4