# Unexpected token in request when using Newman

**URL:** <https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580>\
**Category:** Help Hub\
**Tags:** newman-project, tests, authentication\
**Created:** [June 26, 2022, 4:13pm UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580 "2022-06-26T16:13:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CathyWang](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@CathyWang](https://community.postman.com/u/CathyWang)\
**Post date:** [June 26, 2022, 4:13pm UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/1 "2022-06-26T16:13:09Z")

</div>

> **Archived topic — this may be out of date.**  
> Newman has been replaced by the **[Postman CLI](https://learning.postman.com/docs/postman-cli/postman-cli-overview/)**, Postman’s official, signed command-line tool. It runs collections as Newman did, and also lints API specs, runs mocks and monitors, and syncs local files with Postman from your terminal, CI/CD pipeline or coding agent.  
> Working with an AI coding agent? The [Postman plugin](https://github.com/postmanlabs/postman-plugin) lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.  
> For current help, see the [Postman docs](https://learning.postman.com/docs/), try the free [Postman tools](https://www.postman.com/tools) (no account needed), or join us on [Discord](https://discord.gg/58aNNsRBGR).

_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)_:

- I generated API Key, and get uid for the collection, and uid for the environment according to [newman/README.md at develop · postmanlabs/newman · GitHub](https://github.com/postmanlabs/newman/blob/develop/README.md#using-newman-with-the-postman-api)
- The command to run the collection is like this:  
$ newman run “[https://api.getpostman.com/collections/$uid?apikey=$apiKey](https://api.getpostman.com/collections/$uid?apikey=$apiKey)”   
–environment “[https://api.getpostman.com/environments/$uid?apikey=$apiKey](https://api.getpostman.com/environments/$uid?apikey=$apiKey)”
- I set up token as the environment variables in my collection.
- I can run the same collections in Postman successfully.

_How I found the problem_:

- Run collection via Newman

_I’ve already tried_:

- Run collection in Postman console

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 26, 2022, 4:45pm UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/2 "2022-06-26T16:45:27Z")

</div>

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;

> **[Newman command options | Postman Learning Center](https://learning.postman.com/docs/running-collections/using-newman-cli/newman-options/)**
>
> Newman command options: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

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. 🙃

---

<div class="post-metadata">

**Author:** ![CathyWang](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@CathyWang](https://community.postman.com/u/CathyWang)\
**Post date:** [June 27, 2022, 3:23pm UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/3 "2022-06-27T15:23:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [June 27, 2022, 4:18pm UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/4 "2022-06-27T16:18:17Z")

</div>

When you run this command;

```auto
$ 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?

---

<div class="post-metadata">

**Author:** ![CathyWang](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@CathyWang](https://community.postman.com/u/CathyWang)\
**Post date:** [June 27, 2022, 9:58pm UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/5 "2022-06-27T21:58:27Z")

</div>

I got the uid for collections from [https://api.getpostman.com/collections?apikey=$apiKey](https://api.getpostman.com/collections?apikey=%24apiKey)  
I got the URI for environment from [https://api.getpostman.com/environments?apikey=$apiKey](https://api.getpostman.com/environments?apikey=%24apiKey)  
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

---

<div class="post-metadata">

**Author:** ![docking-module-phys2](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@docking-module-phys2](https://community.postman.com/u/docking-module-phys2)\
**Post date:** [January 17, 2024, 5:56pm UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/6 "2024-01-17T17:56:04Z")

</div>

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

```auto
newman run "FirstAuth.json" -e "Environment.postman_environment" -d "First.csv" -r htmlextra --reporter-htmlextra-export "Path for HTMLReport"

```

```auto
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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 17, 2024, 7:40pm UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/7 "2024-01-17T19:40:59Z")

</div>

Hey @docking-module-phys2 👋

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. 🤔

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.

---

<div class="post-metadata">

**Author:** ![docking-module-phys2](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@docking-module-phys2](https://community.postman.com/u/docking-module-phys2)\
**Post date:** [January 19, 2024, 10:11am UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/8 "2024-01-19T10:11:25Z")

</div>

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** :

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 19, 2024, 10:32am UTC](https://community.postman.com/t/unexpected-token-in-request-when-using-newman/36580/9 "2024-01-19T10:32:55Z")

</div>

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.
