13 days ago
Title: Invalid Authorization Header Error – Bot Deployment Failure
Description:
The bot is encountering an InvalidArgumentError: invalid Authorization header error during deployment. According to the deployment logs, the issue originates from the Undici HTTP client when attempting to process an outgoing HTTP request.
This suggests that the bot's Authorization header is incorrectly formatted, missing, empty, or contains an invalid token. As a result, the bot cannot properly authenticate with the external service or API.
Potential causes:
- Incorrect or missing bot token.
- Authorization header formatted incorrectly.
- Extra spaces or invalid characters in the token.
- Environment variables not configured correctly in the hosting platform.
- Incorrect authentication configuration in the bot's source code.
Requested fix:
Please investigate the bot's authentication configuration, verify that the correct token is being loaded from the environment variables, and ensure that the Authorization header follows the required format.
The goal is to resolve the deployment error and allow the bot to start and authenticate successfully.
Additional contact information:
Discord: thevoiceoftheemperors
2 Replies
13 days ago
This thread has been opened as a bounty so the community can help solve it.
Status changed to Open Railway • 13 days ago
13 days ago
const token = (process.env.DISCORD_TOKEN || process.env.TOKEN || process.env.BOT_TOKEN || '').trim();
if (!token) {
console.error('Missing Discord token!');
process.exit(1);
}
// Optional debug (remove after fixing — never log the full token)
console.log('Token loaded:', Boolean(token), 'length:', token.length);
client.login(token);
13 days ago
This error comes from Undici's header validation, not from Railway or Discord. In lib/core/request.js, processHeader throws invalid ${key} header when the value fails isValidHeaderValue, which rejects anything outside [\t\x20-\x7e\x80-\xff]. So Authorization: Bot <token> is rejected if the token contains a newline, a zero-width character, a smart quote, an ellipsis, or any non-Latin-1 character.
I reproduced it with undici 8.12.4 on Node 22. A trailing "\n" fails but .trim() fixes it. A "\n" inside the value, U+200B, smart quotes, "…" or Cyrillic text fail and .trim() does NOT fix them. Plain ASCII quotes are OK. So a token that passes .trim() and a length check can still fail.
Find the bad character without printing the token:
const token = (process.env.DISCORD_TOKEN ?? '').trim();
const bad = [...token].map((c, i) => [i, c.codePointAt(0)]).filter(([, cp]) => !(cp === 9 || (cp >= 0x20 && cp <= 0x7e) || (cp >= 0x80 && cp <= 0xff)));
console.log('invalid header chars (index, codepoint):', bad);
if (bad.length || /\s/.test(token)) process.exit(1);Then fix it: in Railway, open the service > Variables, delete the variable and re-paste the token as a single line (raw editor, no quotes, no trailing spaces), or reset the token in the Discord Developer Portal and set the new one.
Also check that no other Authorization header in your code is built from a different env var. Any outgoing request with a bad value gives the same message.
If you post the output of the bad array (indexes and code points only, never the token), we can pin down the exact character.
(Disclosure: drafted with AI assistance. The Undici source behavior was checked against the repo and a local test.)






