0

I'm new to both Postman and Newman. I have created my simple test which uses the Environment Variables JSON for some properties values.

It runs fine when the value for this key is hardcoded in the environment.json but it's failing if I'm trying to pass/replace the value for the key from the command-line. I do not have global variable json, and if possible, prefer not to use it.

Here is my command-line: newman run "C:\Users\Automation\Postman\postman_autotest.json" --folder "AUTO" --global-var "client_secret=XXXX" --environment "C:\Users\Automation\Postman\postman_environment.json"

This value is essential for the API to work/connect, thus I'm getting 400 error back.

here is this key in the environment.json

{
  "id": "673a4256-f5a1-7497-75aa-9e47b1dbad4a",
  "name": "Postman Env Vars",
  "values": [
        {
            "key": "client_secret",
            "value": "",
            "description": {
                "content": "",
                "type": "text/plain"
            },
            "enabled": true
        }
    ],
    "_postman_variable_scope": "environment",
    "_postman_exported_at": "2019-04-03T20:31:04.829Z",
    "_postman_exported_using": "Postman/6.7.4"
}
KVN
  • 863
  • 1
  • 17
  • 35
  • What do you mean by pass *I'm trying to pass/replace the value for the key from the command-line*, how can you do that? just curious to know – Divyang Desai Apr 04 '19 at 05:54
  • 1
    @Div, what I meant is that I have the following Variable (environment) `client_secret`. I cannot hardcode the value for this key in the JSON file (security concern). What I'm trying to do is to provide the value for this key when I execute this JSON collection (`postman_autotest.json`) from a command line, like this `--global-var "client_secret=XXXX"` – KVN Apr 04 '19 at 14:32
  • If I'm not mistaken the way I'm doing it - I have to have a global_variable.json. But I'm wondering if there is a way to do it with Environment Var JSON? – KVN Apr 04 '19 at 14:33
  • I'm not sure if there is `global-var` exists, I'm referring the [official doc of Newman](https://learning.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman/) and couldn't find such thing there. However, if you don't want to save it you can remove all sensitive variable information, just after the last request. – Divyang Desai Apr 05 '19 at 06:38
  • Here is the doc that explains more options: https://github.com/postmanlabs/newman#newman-run-collection-file-source-options – KVN Apr 05 '19 at 21:48

1 Answers1

1

Just a thought... You can use a wrapper powershell script to replace the key at runtime then delete the file.

[CmdletBinding()]
Param (
    [Parameter(Mandatory)]
    [string]$Secret
)

$envFile = "C:\Users\Automation\Postman\postman_environment.json"
$envFileWithKey = "C:\Users\Automation\Postman\postman_environment_w_key.json"
$json = Get-Content $envFile -Raw | ConvertFrom-Json

$json.values[0].key = $Secret

ConvertTo-Json $json -Depth 10 | Out-File $envFileWithKey -Force

newman run "C:\Users\Automation\Postman\postman_autotest.json" --folder "AUTO" --environment $envFileWithKey

Remove-Item -Path $envFileWithKey

Then just:

.\RunAutomation.ps1 -Secret "this_is_a_secret_sshhhhhh"
so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50
  • Thanks @so cal cheesehead looks like using global_variables.json more easy option for my current needs. But I'll use your suggestion for my other scripts runs. – KVN Apr 05 '19 at 21:52
  • Sure. I figured. Just thought I throw it out there. – so cal cheesehead Apr 05 '19 at 22:04
  • Thanks! This actually helped me get rolling to update certain values, I created the var within postman, then updated the property value that I needed in my newman call. – Maverick Sevmont Dec 07 '21 at 17:51