How to run newman on a list of files containing request body

Hi there,

Context

I have a list of files (in order of 100s) where each file contains a request body in the form of a list of hashmaps. If I copy-paste the files individually into postman, everything seems to be working well so I’m trying to automate running each of these files to a specific endpoint in my collection using newman.

For that purpose, I decided to write a simple batch script that would iterate over the files in my directory and pass each file to newman run. My batch script is as follows:

@echo off

set JSON_DIR=<EXACT-DIR-LOCATION>

for %%f in (%JSON_DIR%\*.json) do (
    rem add --verbose to get more info out of each call.
    echo %%f
    newman run <COLLECTION_NAME>.json -d "%%f" ^
     --folder "<REQUEST_NAME>" ^
     --iteration-count 1 ^
     --verbose
)

I’m also adding the data snippet from one of the files:

[
	{
		"id": null,
		"geohash": null,
		"latitude": 19.26362,
		"sequence": 0,
		"longitude": 73.07356,
		"googleAddress": null
	},
	{
		"id": null,
		"geohash": null,
		"latitude": 19.26045,
		"sequence": 1,
		"longitude": 73.06872,
		"googleAddress": null
	},
	{
		"id": null,
		"geohash": null,
		"latitude": 19.25907,
		"sequence": 2,
		"longitude": 73.06629,
		"googleAddress": null
	}
]

My POST endpoint is designed to take in a list of such hashmaps and push the data onto a graph database.

Solutions I’ve tried

  1. Keeping the Request Body empty in postman, before exporting it:
    Naturally, I assumed that if I only pass the data file via -d it will automatically add it as the request body (as if it was copy-pasted). But my fastapi service gave the following error:
    Exception has occurred: RequestValidationError [{'type': 'missing', 'loc': ('body',), 'msg': 'Field required', 'input': None, 'url': 'https://errors.pydantic.dev/2.5/v/missing'}] denoting that it may not be working as I expected (either because the values are not getting parsed or passed).

  2. Adding variables in the expected format, before exporting it:
    My request body in Postman looked like the following:

[
    {
        "id": null,
        "latitude": {{latitude}},
        "sequence": {{sequence}},
        "longitude": {{longitude}},
        "googleAddress": null,
        "geohash": null
    }
]

Doing this leads to newman only taking the first element of the list and passing it to the fastapi service, I can confirm this because if I increase the length to two such hashmaps, I get two elements in the request at the endpoint.

Question

  1. If I have multiple files and each file contains variable number of such element in the form of a list, how do I pass all of it to an endpoint as a single request body?
  2. How do I pass variables optionally null variables (like id, geohash, googleAddress)? If I pass it as variable, I get a validation error in case the value is is explicitly null in the file.

The Newman command line option -d is a data file, which means its meant to be used with the Collection Runner where it will run one request at a time. One for each object in the JSON. As the iteration count is 1, it will only run the first object.

If you want to send all of the data in one go instead of individual requests to the API then instead of passing it as a data file, pass the JSON content as an variable and then use this variable as the body for the request.

Not sure you can do this in DOS, and you might need something like PowerShell to read the JSON contents before sending it as an variable.

Another alternative which might not be feasible would be to change the JSON files so they have a leading object that contains the array. Therefore making it a single object which would run with the iteration count of 1. In the pre-request script, you would parse that variable to the array (which should be contained in the special data variable) and then set that as the body.

If the API can’t accept the null values, then you will need to clean the array first and remove those keys.

The following does this for a single object, but you can extend it to cater for the array with an outer and inner loop.

let iterationData = data;
// console.log(iterationData);

for (var key in iterationData) {
    if (iterationData[key] === null) {
        delete iterationData[key];
    }
}

// console.log(iterationData);

pm.request.body.raw = JSON.stringify(iterationData);

Wouldn’t that also involve changing the structure of the request body to be something like:

data: [
  {"key": value},
  {"key": value}
]

followed by passing and accepting it as {{data}} in the request body in postman collection? (which I think is something similar to what you mentioned as an alternative solution)

I’m curious as to why such a case is not already taken care of, I always thought this was a rudimentary case. Is this not one of the standard ways to test the APIs?

Thanks for the reply!

You seemed to indicate that you wanted to send all of the objects in one go, and that the API can support this. This would mean using the array.

Therefore you need to pass the variable as an array.

Although you also mention that it does work with the first object, so you might want to confirm if the API can take an array.

What I’m not sure of is whether you will need to JSON.parse the variable before you can set it as the body or whether you can reference it directly. This would need testing.

You would need to update it as you have described if you used the collection runner method instead.

If the element was called data, you would then target the array using the following.

let body = data.data;
pm.request.body.raw = JSON.stringify(body);

This will replace the body of the request with your array.

That’s correct. My endpoint can receive list[Object] as the request body. I think the reason passing a single object also works is because of the internal validation done by pydantic to convert the incoming request object into a single-element list.

Could you please confirm if I have understood you correctly:

  1. Assuming I’m using PowerShell, I iterate over all the files in the directory
  2. I read the file’s content and store it as a string, which I then pass it to newman using -e (-e data where data is the json string)
  3. In the postman collection, in the pre-request section, I try to parse the string (using JSON.parse) into the array and set it as the request body as pm.request.body.raw.

I apologize if my clarifications seem rather basic, I’m not yet proficient with the mechanics of postman.

If you have a look at the reference guide.

Customize a collection run using Newman command options | Postman Learning Center

You need to scroll down to the β€œMiscellaneous Options” and you will want to send the variable as an individual environment variable.

--env-var "[environment-variable-name]=[environment-variable-value]"

The value would be your PowerShell string.

What I’m not sure of is whether you need to parse the string in Postman, or can just use it directly from the environment variable.

image

Otherwise, you will need to parse the variable, and then set the body that way.

@michaelderekjones

I’ve made some progress but I’m stuck at the last point. I followed your advice and here’s what I’ve done:

Created a PowerShell script to read contents from the JSON file:

$jsonDirectory = "<ABSOLUTE-DIR-LOC>"

foreach ($jsonFile in Get-ChildItem -Path $jsonDirectory -Filter *.json ) {
    $jsonContents = Get-Content -Path $jsonFile.FullName | Out-String

    $newmanCommand = "newman run Polyline_Caching.postman_collection.json --folder `"Import from Waypoint Creation`" --env-var 'DATA=$jsonContents' --verbose"

    try{
        Write-Host $newmanCommand
        Invoke-Expression $newmanCommand
    } Catch {
        Throw "Script failed with the following: $($_.exception.Message)"
    }
}

The output of Write-Host $newmanCommand (essentially a stdout) is:

newman run Polyline_Caching.postman_collection.json --folder "Import from Waypoint Creation" --env-var 'DATA=[
        {
                "id": null,
                "geohash": null,
                "latitude": 19.26362,
                "sequence": 0,
                "longitude": 73.07356,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.26045,
                "sequence": 1,
                "longitude": 73.06872,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25907,
                "sequence": 2,
                "longitude": 73.06629,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25679,
                "sequence": 3,
                "longitude": 73.06294,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25558,
                "sequence": 4,
                "longitude": 73.06165,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25434,
                "sequence": 5,
                "longitude": 73.06005,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25354,
                "sequence": 6,
                "longitude": 73.0587,
                "googleAddress": null
        }
]
' --verbose

Which should work since the format seems correct.

However, newman fails with the following comment:

import
β”” Import from Waypoint Creation
  POST 0.0.0.0:8000/import/ir
  422 Unprocessable Entity β˜… 22ms time β˜… 311B↑ 302B↓ size β˜… 10↑ 4↓ headers β˜… 0 cookies
  β”Œ ↑ raw β˜… 9B
  β”‚ [
  β”‚     {
  β”‚
  β””
  β”Œ ↓ application/json β˜… text β˜… json β˜… utf8 β˜… 158B
  β”‚ {"detail":[{"type":"json_invalid","loc":["body",9],"msg":"JSON decode error","input":{},"ctx":{"error":"Expecting pro
  β”‚ perty name enclosed in double quotes"}}]}
  β””
  prepare   wait   dns-lookup   tcp-handshake   transfer-start   download   process   total
  27ms      5ms    (cache)      4ms             4ms              7ms        303Β΅s     49ms

  1. Status Code is 201
  2. Make sure some nodes are added
  3. Make sure some relationships are added

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         β”‚          executed β”‚          failed β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚              iterations β”‚                 1 β”‚               0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                requests β”‚                 1 β”‚               0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚            test-scripts β”‚                 2 β”‚               0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      prerequest-scripts β”‚                 2 β”‚               0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚              assertions β”‚                 3 β”‚               3 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ total run duration: 201ms                                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ total data received: 158B (approx)                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ average response time: 22ms [min: 22ms, max: 22ms, s.d.: 0Β΅s] β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ average first byte time: 4ms [min: 4ms, max: 4ms, s.d.: 0Β΅s]  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

  #  failure                detail

 1.  AssertionError         Status Code is 201
                            expected response to have status code 201 but got 422
                            at assertion:0 in test-script
                            inside "import / Import from Waypoint Creation"

 2.  AssertionError         Make sure some nodes are added
                            expected undefined to be a number or a date
                            at assertion:1 in test-script
                            inside "import / Import from Waypoint Creation"

 3.  AssertionError         Make sure some relationships are added
                            expected undefined to be a number or a date
                            at assertion:2 in test-script
                            inside "import / Import from Waypoint Creation"

Even if I try to parse it in the Pre-request Script with JSON.parse() I get the same error (at the level of Pre-request script rather than a 422). I’m not sure why this is happening. It’s evident that newman receives properties without any quotations but then the request body sent it to seems to contains double quotes.

Most of the similar problems in the forums are straightforward - and hence not applicable to this case.

I have a few observations.

The first is that you need to have an environment file which you don’t seem to have on your Newman command line. You can’t send it an environment variable if you don’t have an environment selected.

Try this first.

Let’s also have a look at the Newman command line option again.

--env-var "[environment-variable-name]=[environment-variable-value]"

You have…

'DATA=$jsonContents'

Shouldn’t this be

β€œ[DATA]=[$($jsonContents)]”

It looks like you should include the square brackets, which means there should be two square brackets if the value is an array when you Write-Host.

Lastly β€œdata” is a special variable in Postman, so you might want to consider changing the variable name to something else to ensure you don’t get conflicts.

I followed the steps you prescribed:

  1. Created an environment with a variable called IMPORT_WAYPOINT_DATA and enabled and exported it to the directory which contains my powershell script.
  2. I added the square brackets to the env vars as suggested

The script then becomes:

$jsonDirectory = "[DIR]"

foreach ($jsonFile in Get-ChildItem -Path $jsonDirectory -Filter *.json ) {
    $jsonContents = Get-Content -Path $jsonFile.FullName | Out-String
    
    $newmanCommand = "newman run Polyline_Caching.postman_collection.json --folder 'Import from Waypoint Creation' -e env.postman_environment --env-var `"[IMPORT_WAYPOINT_DATA]=[$($jsonContents)]`" --verbose"
    try{
        Write-Host $newmanCommand
        Invoke-Expression $newmanCommand
    } Catch {
        Throw "Script failed with the following: $($_.exception.Message)"
    }
}

Which gives the following output when I try to run the script:

newman run Polyline_Caching.postman_collection.json --folder 'Import from Waypoint Creation' -e env.postman_environment --env-var "[IMPORT_WAYPOINT_DATA]=[[
        {
                "id": null,
                "geohash": null,
                "latitude": 19.26362,
                "sequence": 0,
                "longitude": 73.07356,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.26045,
                "sequence": 1,
                "longitude": 73.06872,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25907,
                "sequence": 2,
                "longitude": 73.06629,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25679,
                "sequence": 3,
                "longitude": 73.06294,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25558,
                "sequence": 4,
                "longitude": 73.06165,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25434,
                "sequence": 5,
                "longitude": 73.06005,
                "googleAddress": null
        },
        {
                "id": null,
                "geohash": null,
                "latitude": 19.25354,
                "sequence": 6,
                "longitude": 73.0587,
                "googleAddress": null
        }
]
]" --verbose
newman

Polyline Caching

β–‘ import
β”” Import from Waypoint Creation
  β”Œ
  β”‚ ''
  β””
  POST 0.0.0.0:8000/import/ir
  422 Unprocessable Entity β˜… 5.1s time β˜… 270B↑ 276B↓ size β˜… 9↑ 4↓ headers β˜… 0 cookies
  β”Œ ↓ application/json β˜… text β˜… json β˜… utf8 β˜… 132B
  β”‚ {"detail":[{"type":"missing","loc":["body"],"msg":"Field required","input":null,"url":"https://errors.pydantic.dev
  β”‚ /2.5/v/missing"}]}
  β””
  prepare   wait   dns-lookup   tcp-handshake   transfer-start   download   process   total
  22ms      4ms    (cache)      3ms             5s               7ms        406Β΅s     5.1s

  1. Status Code is 201
  2. Make sure some nodes are added
  3. Make sure some relationships are added

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         β”‚         executed β”‚           failed β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚              iterations β”‚                1 β”‚                0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                requests β”‚                1 β”‚                0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚            test-scripts β”‚                2 β”‚                0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      prerequest-scripts β”‚                2 β”‚                0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚              assertions β”‚                3 β”‚                3 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ total run duration: 5.2s                                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ total data received: 132B (approx)                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ average response time: 5.1s [min: 5.1s, max: 5.1s, s.d.: 0Β΅s] β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ average first byte time: 5s [min: 5s, max: 5s, s.d.: 0Β΅s]     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

  #  failure                detail

 1.  AssertionError         Status Code is 201
                            expected response to have status code 201 but got 422
                            at assertion:0 in test-script
                            inside "import / Import from Waypoint Creation"

 2.  AssertionError         Make sure some nodes are added
                            expected undefined to be a number or a date
                            at assertion:1 in test-script
                            inside "import / Import from Waypoint Creation"

 3.  AssertionError         Make sure some relationships are added
                            expected undefined to be a number or a date
                            at assertion:2 in test-script
                            inside "import / Import from Waypoint Creation"

My Pre-Request Script is as follows:

let body = pm.environment.get("IMPORT_WAYPOINT_DATA");
console.log(body);
pm.request.body.raw = body;

I believe that square brackets were only present to show that it needs to be inputted but removing that gives the following error:

β–‘ import
β”” Import from Waypoint Creation
  β”Œ
  β”‚ '[\r\n\t{\r\n\t\t'
  β””
  POST 0.0.0.0:8000/import/ir
  422 Unprocessable Entity β˜… 23ms time β˜… 311B↑ 302B↓ size β˜… 10↑ 4↓ headers β˜… 0 cookies
  β”Œ ↑ raw β˜… 9B
  β”‚ [
  β”‚     {
  β”‚
  β””
  β”Œ ↓ application/json β˜… text β˜… json β˜… utf8 β˜… 158B
  β”‚ {"detail":[{"type":"json_invalid","loc":["body",9],"msg":"JSON decode error","input":{},"ctx":{"error":"Expecting
  β”‚ property name enclosed in double quotes"}}]}

Removing whitespace doesn’t help either as I get the JSON decoding error that I previously mentioned.

Try running the Newman command line with a single value in the variable to see how it works.

You can test this against the Postman Echo service instead of a real API.

This will then show you if you have the correct formatting for the command.

Pretty sure its just the formatting of this element that is causing the issue, but without testing it myself, I’m not 100% sure. I don’t have time right now to replicate this.

The array should be a string before you can parse it.

The whole option to pass an environment variable seems to be wrapped in a quote, but I’m not sure the array is being treated as a string like it should be.

I would run this against Postman Echo with a simple string to start with and then just console log the environment variable to see how its being treated.

Once it works with a simple string, I would then send across a simple JSON object with a single key\value pair in it, at which point you should be able to see if you need to parse it or not. Once you have this working, you can then move onto your array before finally integrating it into your PowerShell script.

I’ll probably be able to test this on Wednesday if you are still having issues. Pretty sure this will work with a few tweaks.

Thanks, I made it work with my smaller piece of data (I’ll attach my codes at the end for others to refer.)

However, for bigger files, I’m getting the following error:

Script failed with the following: Program 'node.exe' failed to run: The filename or extension is too
longAt C:\Users\sagar\AppData\Roaming\npm\newman.ps1:24 char:5
+     & "node$exe"  "$basedir/node_modules/newman/bin/newman.js" $args
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
At C:\Users\sagar\Work\automate-postman-polyline-caching\run_polyline_points.ps1:14 char:9
+         Throw "Script failed with the following: $($_.exception.Messa ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Script failed w...~~~~~~~~~~~~~~.:String) [], Runtime
   Exception
    + FullyQualifiedErrorId : Script failed with the following: Program 'node.exe' failed to run: The
   filename or extension is too longAt C:\Users\sagar\AppData\Roaming\npm\newman.ps1:24 char:5
+     & "node$exe"  "$basedir/node_modules/newman/bin/newman.js" $args
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.

I guess this is happening because the resulting command exceeds the character limit. Do you know if there’s any workaround?


My Powershell script:

$jsonDirectory = "[DIR]"

foreach ($jsonFile in Get-ChildItem -Path $jsonDirectory -Filter *.json ) {
    $jsonContents = Get-Content -Path $jsonFile.FullName | Out-String
    $jsonContents = $jsonContents -replace '\s',''
    $jsonContents = $jsonContents.Replace('"', "'")

    $folderName = "Import from Waypoint Creation"
    $envVar = "IMPORT_WAYPOINT_DATA=$($jsonContents)"

    try{
        newman run collection.postman_collection --folder $folderName -e env.postman_environment --env-var $envVar
    } Catch {
        Throw "Script failed with the following: $($_.exception.Message)"
    }
}

I keep the Body of the collection empty and feed the request body via Pre-Request Script:

let body = pm.environment.get("IMPORT_WAYPOINT_DATA").replace(/'/g, '"');
pm.request.body.raw = JSON.parse(body);

All of these work for smaller files but fail for slightly bigger ones (47KB).

Can you minify the JSON file in the PowerShell script.

That might reduce the size to something acceptable.

Try an online tool first to see if this works.

JSON Minify Online to Minify, Compact JSON Data and Best (jsonformatter.org)

The following has details on how to achieve this in PowerShell.

Minify JSON with PowerShell? - Stack Overflow

This might work on medium-sized files (10-100KB) but I’ve also got files of around 1500KB-4000KB. I’m not sure it would work in those cases. I’ll try it regardless and get back here with updates.

BTW, do you know where exactly this limitation is coming from?

Not sure if its Newman or PowerShell\Windows with the dreaded 255 character path limit.

Another option here is to read the file in PowerShell and check the size of the array and split it into smaller arrays that you could then loop through which will hopefully keep it under any limits.

If the file size is still an issue, I suspect it will be easier to change the structure of the JSON file so that is is a object with a single key\value pair, with the value being the jsonContents array.

You already know how to read the JSON files in PowerShell. You just need to create a new object using the jsonContents, and then export this so it can be used as a data file (which hopefully won’t have the same limitations). You won’t need the environment then either.

Sounds like a feasible solution, I’ll try this and get back to you.

I’ve modified the code as following:

$jsonDirectory = "[DIR]"

$jsonCollectedDirectory = "$jsonDirectory\collected"

If(!(Test-Path -Path $jsonCollectedDirectory)){
    # Create a new directory called `collected` inside the passed directory
    New-Item -Path $jsonCollectedDirectory -ItemType Directory
}

# Prepare the JSON to be inside a key called "jsonContents"
foreach ($jsonFile in Get-ChildItem -Path $jsonDirectory -Filter *.json ) {
    $jsonObject = Get-Content -Path $jsonFile.FullName | Out-String | ConvertFrom-Json

    # Just to check whether we can retrieve the content inside the json
    # foreach($element in $jsonObject){Write-Host $element.latitude}

    # Make an array containing a single element hashtable
    $newJsonObject = @(
        @{"jsonContents" = $jsonObject}
    )

    # Save the new Json Object in the `collected` directory.
    $newJsonObject | ConvertTo-Json | Out-File "$jsonDirectory\collected\$jsonFile"
}


# Iterate over the collected JSON file and call `newman`
$folderName = "Import from Waypoint Creation"
foreach ($jsonFile in Get-ChildItem -Path $jsonCollectedDirectory -Filter *.json) {
    try{
        Write-host $jsonFile.FullName
        newman run collection.postman_collection --folder $folderName -d $jsonFile.FullName --verbose
    } Catch {
        Throw "Script failed with the following: $($_.exception.Message)"
    }
    
}

The intermediate JSON is of the following format:

{
    "jsonContents":  {
                         "value":  [
                                       {
                                           "id":  null,
                                           "geohash":  null,
                                           "latitude":  19.26362,
                                           "sequence":  0,
                                           "longitude":  73.07356,
                                           "googleAddress":  null
                                       },
                                       {
                                           "id":  null,
                                           "geohash":  null,
                                           "latitude":  19.26045,
                                           "sequence":  1,
                                           "longitude":  73.06872,
                                           "googleAddress":  null
                                       },
                                       {
                                           "id":  null,
                                           "geohash":  null,
                                           "latitude":  19.25907,
                                           "sequence":  2,
                                           "longitude":  73.06629,
                                           "googleAddress":  null
                                       },
                                       {
                                           "id":  null,
                                           "geohash":  null,
                                           "latitude":  19.25679,
                                           "sequence":  3,
                                           "longitude":  73.06294,
                                           "googleAddress":  null
                                       },
                                       {
                                           "id":  null,
                                           "geohash":  null,
                                           "latitude":  19.25558,
                                           "sequence":  4,
                                           "longitude":  73.06165,
                                           "googleAddress":  null
                                       },
                                       {
                                           "id":  null,
                                           "geohash":  null,
                                           "latitude":  19.25434,
                                           "sequence":  5,
                                           "longitude":  73.06005,
                                           "googleAddress":  null
                                       },
                                       {
                                           "id":  null,
                                           "geohash":  null,
                                           "latitude":  19.25354,
                                           "sequence":  6,
                                           "longitude":  73.0587,
                                           "googleAddress":  null
                                       }
                                   ],
                         "Count":  7
                     }
}

I’ve also modified the Pre-request Script to be like:

let recievedRequest = pm.request.body.raw;
pm.request.body.raw = receivedRequest["jsonContents"]["value"];

But upon running, I find that an empty request body is being passed. I tried converting the intermediate JSON format as [{}] but I’ve failed to do so.

The reason I think that the data file requires an array of objects is because of this example.

The data file does need to be an array.

Working with data files - data file format

What you need is an array with a single object in it.

That object will have a single key and the value will be your array.

[
    {
        "jsonContents": [
            {
                "id": null,
                "geohash": null,
                "latitude": 19.26362,
                "sequence": 0,
                "longitude": 73.07356,
                "googleAddress": null
            },
            {
                "id": null,
                "geohash": null,
                "latitude": 19.26045,
                "sequence": 1,
                "longitude": 73.06872,
                "googleAddress": null
            }
        ]
    }
]

That is exactly what I’ve been trying to do with my code above but for some reason, I can’t figure out why it is excluding the initial square brackets. Do you know
ConvertTo-Json on $newJsonObject is not working?

Probably do with with this line.

If the file content being picked up are already JSON, why do you need to convert them?