Heroku Config with Special Characters (Specifically Ampersands)

Viewed 882

I have a Heroku application that needs a URI with an ampersand (&) in it as a config variable. Specifically, it's a MongoDB Atlas connection string, but that's beside the point. According to the Heroku Docs, I should format this CLI command like so:

heroku config:set exampleVar=www.bar.com?a=b&c=d&e=f&justkey

However, when I attempt to actually run that command, I get the following:

Setting exampleVar and restarting ⬢ example-heroku-app... done, v10
exampleVar: www.bar.com?a=b
'c' is not recognized as an internal or external command,
operable program or batch file.
'e' is not recognized as an internal or external command,
operable program or batch file.
'justkey' is not recognized as an internal or external command,
operable program or batch file.

It appears that the & character is causing Heroku to interpret what comes after it as a command? I'm guessing I need to somehow escape it, but I'm at a loss as to how. Converting & into \& has no effect, and neither does adding single or double quotes. If anyone has an answer here, please enlighten me! I can't seem to find decent documentation on Heroku Config's special character rules.

3 Answers

Wrap the & character in double quotes. below is an example.

heroku config:set MONGODB_URL='mongodb+srv://hello:test@cluster0.y6red.mongodb.net/fdert?retryWrites=true"&"w=majority'

Please note the use of single quotes and double quotes.

The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as
part of a string.

Had a similar struggle a while ago. I was using Git Bash in a Windows environment and all attempts to get the ampersand parsed as intended proved futile. I tried using single, double quotes, forward-slashes, and backslashes in all possible combinations to no avail.

What worked for me was switching from Git Bash to Windows Terminal. I had to wrap the ampersand character in double quotes and the entire string in single quotes.

Related