Unexpected token in request when using Newman

My question:
I got “token ‘U’ at 1:1 Unauthorized request” when I use Newman to run my collection.
JSONError Unexpected token ‘U’ at 1:1
Unauthorized request
^
Details (like screenshots):

How I found the problem:

  • Run collection via Newman

I’ve already tried:

  • Run collection in Postman console

Hi @CathyWang

This may not completely resolve the issue but if you are going to use the “long notation” I think you are missing a - from “environment”.

Should be either -e or --environment (Note the 2x - for the latter)

More info here;

I’m not 100% sure if you can call variables from a file you are also trying to load at the same time… but if you try the dash thing above first and report back, I will look into this further if needed. :upside_down_face:

Thank you for the response!
Yes, I did use --environment in the command. I didn’t call variables from a file. I just set the environment variables and got the environment URI from: https://api.getpostman.com/environments?apikey=$apiKey.

Regards,
Cathy

When you run this command;

$ newman run “[https://api.getpostman.com/collections/$uid?apikey=$apiKey](https://api.getpostman.com/collections/%24uid?apikey=%24apiKey)”
–environment “[https://api.getpostman.com/environments/$uid?apikey=$apiKey](https://api.getpostman.com/environments/%24uid?apikey=%24apiKey)”

Where are the values for $uid and $apiKey stored?

I got the uid for collections from https://api.getpostman.com/collections?apikey=$apiKey
I got the URI for environment from https://api.getpostman.com/environments?apikey=$apiKey
I created the apiKey in Postman.

I just passed the above value to the newman command line. Where these values should be stored?

Thanks,
Cathy

Hello,

I am facing same issue.
I have multiple collections and I have to run them in sequence.

In Postman:

  1. I have environment variables.
  2. First collection (which is a authorization collection) sets the value in global variable and gets its from csv as an input.
  3. Second collection takes the value from global variable and gets its data input from csv as an input.
  4. I run first collection and then second collection.
    It works perfect in postman.

In Newman

  1. I have exported environment variables and each collection separately
  2. I executed below commands in sequence on command line
newman run "FirstAuth.json" -e "Environment.postman_environment" -d "First.csv" -r htmlextra --reporter-htmlextra-export "Path for HTMLReport"
newman run "Second.json" -e "Environment.postman_environment" -g "workspace.postman_globals.json" -d "Second.csv" -r htmlextra --reporter-htmlextra-export "Path for HTMLReport"

After executing above commands on command line with or without -r htmlextra --reporter-htmlextra-export "Path for HTMLReport"

I get below below error for the second command:

Unexpected token ‘U’ at 1:1
Unauthorized
^

Can anyone please give their suggestion what needs to be done to resolve this.

Hey @docking-module-phys2 :wave:

Welcome to the Postman community :postman:

I can’t really see how that would work and set any global value that the second run could use here. Those are both independent runs. :thinking:

Within the app, you’re doing all this in a Workspace and those variables are stored at the global level in the current value (If that’s set via a script). Even if the Globals were exported, those current values wouldn’t be in that exported JSON file.

It’s worth opening up those exported files in a text editor to check the contents.

What variables are actually used during the Collection Run?

You should be able to see that in the report when using the --reporter-htmlextra-showEnvironmentData and --reporter-htmlextra-showGlobalData flags.

Hey @danny-dainton

Thanks for replying.

Yes, I tried --reporter-htmlextra-showEnvironmentData and --reporter-htmlextra-showGlobalData flags and found second collection is not taking Globals set runtime from first collection.

I have requirement where there are multiple collections and each collection has its own csv as an input. First collection generates access token, some other parameters and all other collections should take those globals set i.e. ‘access token’, other parameters’ as an input to generate their respective results.

I even tried below in Newman:

newman run "FirstAuth.json" -e "Environment.postman_environment" -d "First.csv"  --export-environment "Path for Enviroment.json" -r htmlextra --reporter-htmlextra-showEnvironmentData --reporter-htmlextra-showGlobalData --reporter-htmlextra-export "Path for HTMLReport"

In above command I have exported environment and in below command have take exported environment as an input

newman run "Second.json" -e "Exported Environment Json" -d "Second.csv" -r htmlextra --reporter-htmlextra-showEnvironmentData --reporter-htmlextra-showGlobalData --reporter-htmlextra-export "Path for HTMLReport"

Report generated for second command shows environment variable generated from first command.

Still I am getting same Json Error.

Any suggestion what needs to be done to resolve this.

What does the request data (Request Params/Request Headers/Request Body) look like for those failed ones - Are the values from the environment file actually getting set correctly?

Do you still have references to getting those values from the global scope in the Collection?

Without more information and seeing what you have in place, it’s difficult to know what’s going on here. Just looking at those commands doesn’t really give you the full picture.