Home > Mobile >  Terminal throwing error around ampersand (&) on curl request
Terminal throwing error around ampersand (&) on curl request

Time:11-12

I am trying to do a pretty simple curl request to a public API using Terminal on a Mac running Big Sur and zsh as my shell. Here's the command:

https://api.nasa.gov/neo/rest/v1/feed?start_date=2021-11-11&end_date=2021-11-11&api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I keep getting this error message:

zsh: parse error near `&'

I don't understand what it means as there are no quotes in the command. I tried wrapping the key in quotes to see if that would resolve it, same message, then tried wrapping the dates and keys, same thing, switched the order of the arguments, same.

Would appreciate any help on this.

CodePudding user response:

You should quote the "offending" argument, since it contains shell metacharacters

curl https://www.google.com/search?q=test&ie=utf-8&oe=utf-8&client=firefox-b-ab

should become

curl " https://www.google.com/search?q=test&ie=utf-8&oe=utf-8&client=firefox-b-ab"

You can use single or double quotes, or you can escape (prefixing with a blackslash) only the & and the ? character, since they both are shell metacharacters, like this:

curl https://www.google.com/search\?q=test\&ie=utf-8\&oe=utf-8\&client=firefox-b-ab

You can read more about shell metacharacters here:

http://faculty.salina.k-state.edu/tim/unix_sg/shell/metachar.html

I hope to have been helpful, best regards

  • Related