I am trying to use curl to send an image to an API, but while using it with variables within a shell scripts, I am getting an error.
curl: (26) Failed to open/read local data from file/application
The code looks like this:
DATE_NOW=$(date "+%Y%m%d_%H%M%s")
curl -k -4 -v -X POST -H "Authorization: Bearer $API_KEY" -F "file=@/home/user/Documents/ShareX/Screenshots/2023/image_"$DATE_NOW".jpg" $API_URL
I suspected
"$DATE_NOW"
to be the issue but using echo as shown below in the same script is replacing all variables correctly. When I use the command without any variables separately in a command line, it correctly uploads the file.
echo "curl -k -4 -v -X POST -H "Authorization: Bearer $API_KEY" -F "file=@/home/user/Documents/ShareX/Screenshots/2023/image_"$DATE_NOW".jpg" $API_URL"
I have tried to add curly braces like
"${DATE_NOW}"
but it has the same result. I have looked at several posts including
When to wrap quotes around a shell variable
How to pass a variable in a curl command in shell scripting
and several others but have failed to solve the problem.