'How to call the Telegram Bot API `setMyCommands` using curl?

I'm trying to call the setMyCommands API via curl according to this answer. I first tried

curl -L -X "POST" \
    "https://api.telegram.org/bot$TELEGRAM_TOKEN"'/setMyCommands?commands=[{ "command": "command", "description": "description" }]'

curl printed the following error.

curl: (3) bad range specification in URL position

Then I tried to use curl with --globoff flag.

curl --globoff -L -X "POST" \
    "https://api.telegram.org/bot$TELEGRAM_TOKEN"'/setMyCommands?commands=[{ "command": "command", "description": "description" }]'

Now it printed the following error.

curl: (3) URL using bad/illegal format or missing URL

What is the correct way to call it?



Solution 1:[1]

I guess you are using Windows. Try this:

curl -X POST -H "Content-Type: application/json" -d "{\"commands\":[{\"command\":\"test\",\"description\":\"description with spaces\"}]}" https://api.telegram.org/bot$TELEGRAM_TOKEN/setMyCommands

The trick is to escape double quotes in the JSON with a backslash. It would work even for the command you are using, sending the commands in the query, but using -d (shortcut for --data) option makes it easier to include spaces in the description of the commands.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 druskacik