1

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.

elix
  • 21
  • 3
  • add `set -x` before `curl ...` to see what's happening. – pynexj Jul 03 '23 at 06:38
  • @pynexj Using `set -x` just before `curl...` gives this output. `++ curl -k -4 -v -X POST -H 'Authorization: Bearer hiddenAPIKey' -F file=@/home/user/Documents/ShareX/Screenshots/2023/image_20230703_12201688367021.jpg https://api.nightlight.gg/v1/upload curl: (26) Failed to open/read local data from file/application` – elix Jul 03 '23 at 06:56
  • Your `echo` experiment is inconclusive; you had to omit the quotes at beginning and end. – Armali Jul 03 '23 at 07:35
  • @elix so does the file `/home/user/Documents/ShareX/Screenshots/2023/image_20230703_12201688367021.jpg` exist? – pynexj Jul 03 '23 at 09:04
  • 1
    @pynexj i was taking a screenshot created using spectacle as the file, and when i checked the file was there and when i ran the command manually with the file name, it worked. However, your comment got me thinking that maybe the file creation took time, and so I added a delay of 1 second after the screenshot command and it seems to have worked, the uploads are now going through. Thank you for the comment. :) – elix Jul 03 '23 at 09:34
  • Perhaps `DATE_NOW` contains a white space; did you check this? – user1934428 Jul 03 '23 at 10:50
  • @user1934428 `DATE_NOW` just creates a date in the specified format, as shown in the original code. No, it doesn't contain any whitespace or even special characters (just one underscore). – elix Jul 03 '23 at 17:08
  • @Armali I tried removing the quotes at the beginning and end of echo but it was giving me the same result, the variables were being properly replaced. – elix Jul 03 '23 at 17:08
  • You would see the difference the quotes make if you used `printf %s\\n` rather than `echo` (just a side note). – Armali Jul 03 '23 at 17:12

1 Answers1

1

I was taking a screenshot using Spectacle (on KDE, Debian 12) as the file for cURL. However, the file creation probably took some time, and the cURL got executed before the file was created. Adding a sleep 1s to the code after the spectacle command resolved the issue and uploads are now working.

elix
  • 21
  • 3