How to expand a filename inside a curl request - bash

I need to do a curl upload with a file where I don't know the exact file name.
"curl
-F \"status=2\"
-F \"notify=1\"
-F \"ipa=#${FILE}\"
-F \"teams=${TEAM_ID}\"
-H \"X-HockeyAppToken: ${APITOKEN}\"
https://rink.hockeyapp.net/api/2/apps/${APPVERSION}/app_versions/upload"
This is done in gitlab-ci and the FILE variable is set to build/com.test.app_v*.ipa. The file I want to upload has a version number set and has the path build/com.test.app_v1.0.0.0.ipa. The problem I have now is that the * does not get expanded inside this curl call. I've tried it with an export before:
- export ABSOLUTE_FILENAME=${FILE}
- "curl
-F \"status=2\"
-F \"notify=1\"
-F \"ipa=#${ABSOLUTE_FILENAME}\"
-F \"teams=${TEAM_ID}\"
-H \"X-HockeyAppToken: ${APITOKEN}\"
https://rink.hockeyapp.net/api/2/apps/${APPVERSION}/app_versions/upload"
Still I'm gettin an error curl: (26) couldn't open file "build/com.test.app_v*.ipa" How can I expand the path to an absolute path before my curl upload?

With realpath command:
...
-F \"ipa=#$(realpath $FILE)\"
...

Related

Sending file using CURL in windows

I'm trying to send a file using curl in windows.
Here's the command i'm using:
C:\curl>curl -X POST -F chat_id=#telegramchannel -F photo=#IMAGE.png https://api.telegram.org/bot812312342:XXXXXXXXXXXXXXXXXXXXXX/sendPhoto
and I keep getting this error:
curl: (26) Failed to open/read local data from file/application
does anybody know how to solve it and how to use the -F properly with files on windows?
Thanks
If telegramchannel is not a file, then you have to escape # with a backslash or use single quotes to encapsulate the content. As # has special meaning in curl context,
either
curl -X POST -F chat_id='#telegramchannel' -F photo=#IMAGE.png https://api.telegram.org/bot812312342:XXXXXXXXXXXXXXXXXXXXXX/sendPhoto
or
curl -X POST -F chat_id=\#telegramchannel -F photo=#IMAGE.png https://api.telegram.org/bot812312342:XXXXXXXXXXXXXXXXXXXXXX/sendPhoto

Problem to upload images with variables with Twurl - BASH SCRIPT

twurl -H upload.twitter.com "/1.1/media/upload.json" -f ${image} -F media -X POST
This return ERROR: File not found
But if i do a echo "twurl -H upload.twitter.com "/1.1/media/upload.json" -f ${image} -F media -X POST"
And then copy the result in the terminal there is no problem
Result of the echo:
{"media_id":1205943402870779904,"media_id_string":"1205943402870779904","size":56477,"expires_after_secs":86400,"image":{"image_type":"image\/jpeg","w":990,"h":557}}
Thank you all.
The issue is that ~ in image is interpreted as regular string instead of $HOME variable.
You probably setting your image variable like this:
image="~/Downloads/example.jpg"
That way ~ is not resolved.
You could remove quotes and set your variable this way:
image=~/Downloads/example.jpg
But the best approach will be to replace ~ with ${HOME} or provide full path.
image="${HOME}/Downloads/example.jpg"

Using variables in CURL

I'm using curl to submit a file unto Skyling and then get the output provided by the site (following this: http://skylign.org/help#api_docs ).
I would want to use a variable file input, and not have the write the file each time directly on the code.
When I write the file (hmmfile.hmm) directly onto the code the output is the one expected. But when I set a variable (HMM) and use the variable as my file then the putput is blank.
This works:
curl -H 'Accept:application/json' -F file='#hmmfile.hmm' -F processing=hmm http://skylign.org
This doesn't:
HMM=$1
curl -H 'Accept:application/json' -F file="${HMM}" -F processing=hmm http://skylign.org
The output should be something like this:
"url":"http://skylign.org:8000/logo/6BBFEB96-E7E0-11E2-A243-DF86A4A34227",
"uuid":"6BBFEB96-E7E0-11E2-A243-DF86A4A34227",
"message":"Logo generated successfully"
You should try…
HMM=$1
curl -H 'Accept:application/json' -F file=#"${HMM}" -F processing=hmm http://skylign.org
# ^
# |
# |
The # is needed as it tells curl the field is a file upload.

Creating a loop with curl -F argument

I have this bash script code:
#!/bin/bash
FILES=/home/user/Downloads/asd/
for f in $FILES
do
curl -F dir="#/home/user/Downloads/asd;$f" -F Match=3 -F "Name=DrKla" \
-F countNo=1 -F outputFormat=json "http://somelink.com"
done
Inside the asd folder there are 6 files and I want them to be uploaded 1 by 1 with this code as an argument of -F "dir=#...."
When I run my code I get the error:
Warning: skip unknown form field: /home/user/Downloads/asd/
curl: (43) A libcurl function was given a bad argument
Here is a working version of the code for a single file:
curl -F dir="#/home/user/Downloads/asd/count.txt" -F Match=3 -F "Name=DrKla" \
-F countNo=1 -F outputFormat=json "http://somelink.com"
So I want to have all the files in asd folder to be read and uploaded like this. I don't see what's wrong with my do loop.
The issues appear to be that you only give a path, not a reference to all files in the path * and there is a strange semi-colon ; in your path:
#!/bin/bash
FILES=/home/user/Downloads/asd/*
for f in $FILES
do
curl -F dir="#$f" -F Match=3 -F "Name=DrKla" \
-F countNo=1 -F outputFormat=json "http://somelink.com"
done
I'm not sure what the # is for or if it is needed, but $f should already contain the path.

Windows curl Batch file

I want to make a mailgun curl call using windows batch file. Since windows shell doesn't support multiple lines, how can I execute the below curl function in windows batch file?
curl -s --user 'api:key-xxxxxxxxxx' \
https://api.mailgun.net/v3/sandboxbxxxxxxxxxxxxx.mailgun.org/messages \
-F from='user <email#gmail.com>' \
-F to='user <email#live.com>' \
-F subject='Hello' \
-F text='body!' \
-F attachment=#test.txt \
Update
When I tried to execute the command after removing the multiple lines it returned this error:
curl -s --user 'api:key-xxxxxxxxxx' https://api.mailgun.net/v3/sandboxbxxxxxxxxxxxxx.mailgun.org/messages -F from='user -F to='user -F subject='Hello' -F text='body!' -F attachment=#test.txt 0<email#live.com 1>'
The system cannot find the file specified.
PS: The attachment file is in the same directory
Thanks!
simply on one line and put the <> redirection char between " or escape it with ^:
curl -s --user 'api:key-xxxxxxxxxx' https://api.mailgun.net/v3/sandboxbxxxxxxxxxxxxx.mailgun.org/messages -F from="user <email#gmail.com>" -F to="user <email#live.com>" -F subject='Hello' -F text='body!' -F attachment=#test.txt
You can also create variable for each element :
set "$ApiKey=api:key-xxxxxxxxxx"
set "$Url=https://api.mailgun.net/v3/sandboxbxxxxxxxxxxxxx.mailgun.org/messages"
set "$From=email#gmail.com"
....
and then
curl -s --user '%$ApiKey%' %$Url% -F from="user <%$From%>" -F to= ....

Resources