Newman library get request Error: getaddrinfo ENOTFOUND {{url}}{{basepath}}

I’m trying to run a collection of tests using node and the newman library.

Here’s my script:

const newman = require('newman');
newman.run({
collection: require('.\\Orders_1.postman_collection.json'),
enviornment: require('.\\Innovations odc Lab01.postman_environment.json'),
reporters: 'html',
reporter: {
    html: {
        export: '.\\htmlResults.html',
    }
}
}, function (err) {
	if (err) { throw err; }
console.log('collection run complete!');
});

These two files are in the same folder as the script I’m running.
collection: require(’.\Orders_1.postman_collection.json’),
enviornment: require(’.\Innovations odc Lab01.postman_environment.json’),

I get the report and the “collection run complete!” message and no other errors in the cli.

BUT my tests in the collection are failing because it can’t find the addrinfo.
Error: getaddrinfo ENOTFOUND {{url}}{{basepath}}

The url and basepath variables are in the enviornment json file along with valid keys.

Any idea what might be going wrong here?

Thanks,
Rob

1 Like

Your script says enviornment so that’s probably why it’s not getting that file.

Yeah, that was it. I guess syntax highlighting isn’t available for newman scripting in VS Code.

Thank you!

BTW Do you use the newman library by any chance?

We’re in the initial stages of creating our API (as well as the tests) and I’m looking for the easiest way to run collections via script and maintain them.

I’m leaning towards using the library as working with newman cli and batch scripts seems sub-optimal.

Although, I’m using file exports to run my newman scripts, which is too much work.

Do you have any tips for a newbie?

I would have expected that to be flagged at the very least, as a typo (depends what you’re VSCode setup is like) but there should also be the auto completion coming from the Newman library with would tell you what properties can be added to that run object.

To use the most upto versions of your Collections I would opt for using the Postman API. Using the direct link to the collection would aviod having to download/export the static files each time you make a change.

Yes, that’s the direction I want to go.

Do you happen to know of any examples of how this would be coded using the newman library?

You would just replace you’re static require statement with the URL of the Collection, using the Postman API.

It would be something like this:

const newman = require('newman');
newman.run({
collection: "https://api.getpostman.com/collections/{{collection_uid}}?apikey={{API_TOKEN}}",
environment: require('.\\Innovations odc Lab01.postman_environment.json'),
reporters: 'htmlextra',
reporter: {
    htmlextra: {
        export: '.\\htmlResults.html',
    }
}
}, function (err) {
	if (err) { throw err; }
console.log('collection run complete!');
});

You can also use the environment URL from the Postman API in the same way.

I tried this, but I keep getting an error that it’s an invalid URL.

If I hardcode it, it works, but not using the {{enviroment variable}} syntax.

const newman = require('newman'); 

newman.run(
{
 collection: 'https://api.getpostman.com/collections/6267034-a257e524-c6f8-487b-8185- 
 fb633e60781a?apikey={{postman_api_key}}',// <-----Doesn't work
environment: require('.\\Innovations odc Lab01.postman_environment.json'),
reporters: 'htmlextra',  
reporter: 
{
    htmlextra: 
    {
        export: '.\\Results.html',
        template: '.\\template.hbs',
        title: 'Innovations API',
        showEnviornmentData: true,
        showMarkdownLinks: true,
        showFolderDescription: true
    }
   }
 }, function (err) {
if (err) { throw err; }
    console.log('collection run complete!');
     });

Error: collection could not be loaded
** Error fetching the collection from the provided URL. Ensure that the URL is valid.**
** Invalid API Key. Every request requires a valid API Key to be sent.**

The postman_api_key is in the enviornment.json.

Also, is there some documentation that covers this?
The only newman docs I found are on their Github page and it’s not all that great.

That’s not going to know what that api key is, if you use it like that.

You’re basically asking your node script to look into the environment file to find an API Key in a format only known, to a Collection.

Outside of a Collection, that {{..}} syntax is unknown. You’re working with a node JS script there so you would need to reference that key value with a variable known to that script or hardcode it.

Is there any way to retrieve the key via the postman library?

It seems like a huge hole in functionality if the newman library can’t retrieve variables from the collections and enviornments.

That’s just a URL that happens to return your Postman Collection as a JSON response. The URL could be anything that returns that known format doesn’t need to be Postman specific.

You’re dealing with a node script there and using that syntax just wouldn’t work that way.

It would be the same as if the node script did something else completely different that was nothing to do with Postman.

You can hardcode the value, store it as a variable, use something like the dotenv module as a config file, read the value from a system environment file but you can’t directly look into the JSON file in the same way.

You could read the JSON file with fs and extract out those values with vanilla JS but you can’t use {{..}} in the URL link because it would just be sending that complete string as the URL.

If you have feature requests and issues relating to the documentation, I would encourage you to raise them against the Newman repo so we can action those in the right place.

Ok. So are you associated with newman?

What I’m wondering is if we can access PM variables using the newman library?

I couldn’t tell if you answered that in your response so I can’t really post on the newman repo if that’s a feature you do in fact need.

Is the dotenv module a newman thing? I searched the newman repo, postman (community and documentation) and I couldn’t find any references to that.

If dotenv is a newman thing that should be documentated.

Thanks,
Rob

P.S. Here’s a snippet from newman documentation.

options.environment
One can optionally pass an environment file path or URL as string to this property and that will be used to read Postman Environment Variables from. This property also accepts environment variables as an object . Environment files exported from Postman App can be directly used here.

Optional
Type: object\|string

I want to do the thing in bold. Access the environment variables.

I work for Postman, which created Newman so…yes, I’m associated with it. :grin:

You cannot access Postman variables (which in this context are values in a JSON file) directly in that script, without reading the JSON file in someway to extract the data.

I believe there is a lack of JS/node knowledge happening here and what can and cannot be achieved within a node script.

The dotenv module is something that can be used alongside Newman as a library to abstract the way you reference certain config details.

The URL as a string would just be exactly what you’re doing with that URL to read the Collection file from the Postman API. That data set doesn’t need to come from the API, those can come from any public API that returns the environment values in that format.

This provides example of how you could reference variables in that newman run object:

Take a look at the Newman repo and raise these gaps on there, it’s a more targeted place that a thread on the community forum. It will be easier to group certain issues together as this thread is getting slightly pulled in multiple directions.

Ok, I’ll check those out.

Also, I’m having issues deciphering the doc on the page https://github.com/postmanlabs/newman#newmanrunevents

For this option:
options.iterationData
Path to the JSON or CSV file or URL to be used as data source when running multiple iterations on a collection.

Optional
Type: string

Looking at the example lower on that page.

  newman.run({
  collection: require('./sample-collection.json'),
  iterationData: [{ "var": "data", "var_beta": "other_val" }], <--- This line 
  globals: {
    "id": "5bfde907-2a1e-8c5a-2246-4aff74b74236",
    "name": "test-env",
    "values": [

What in the options.iterationData tells me I can use an array?

All I see is the one sentence description “Path to JSON or CSV…”
and that it’s of type string.

Thanks for your help!

The iterationData value can be like it is in that example or that same array format in a local file and referenced in iterationData using require('data.json').

You could also use a URL, as a string which points to something that returns the data in that JSON format. Basically, that’s the format it needs to be in, how you reference that in the object is the thing that will be changing.

A CSV file can also be used as a source of data.

If the readme doesn’t make sense or the example is not giving you the necessary information you need to use the library, please do file an issue on the repository.

I feel like I’ve told that a few times now :grin: