curl call with parameters into variable doesn't work - shell

I'm working with installr API.
I'm trying to do the following curl request via a script :
curl -H "X-InstallrAppToken: mytoken" https://www.installrapp.com/apps.json/ \
-F 'qqfile=#'$APKPATH \
-F 'releaseNotes=These are my release notes' \
-F 'notify=true'
and it works perfectly.
However, when I try to get my release notes from a file with a variable like this :
RELEASENOTES=`cat "release_notes/test.md"`
curl -H "X-InstallrAppToken: mytoken" https://www.installrapp.com/apps.json/ \
-F 'qqfile=#'$APKPATH \
-F 'releaseNotes='$RELEASENOTES \
-F 'notify=true' > /dev/null
it doesn't work at all, only the first word is sent. For the others, I have the error Could not resolve host: xxx.
I did a echo on these two curl request and the exact same thing is printed.
is that the catcommand which return a specific format ?

Probably an issue with the quotes and spaces. You can use double-quotes around a variable to allow variable expansion in the shell.
RELEASENOTES=$(cat "release_notes/test.md")
curl -H "X-InstallrAppToken: mytoken" https://www.installrapp.com/apps.json/ \
-F "qqfile=#${APKPATH}" \
-F "releaseNotes=${RELEASENOTES}" \
-F 'notify=true' > /dev/null

Related

Multiline GET request using cURL

I want to format a valid GET request in a way it is more human readable. There are many parameters and might be more in the future. So in case of a documentation, how would you format a long URL in a cURL command ? Preferably exhibiting each queryparam in a newline.
curl --header 'header1: XXXXXX' 'https://localhost/api/similar?word=science&target=en&target=fr&target=bg&target=ar&target=fr&source=en&limit=10&score=0.5'
Please note the repeating param target which is valid (and is interpreted as an array depending on the query parser on server side).
Edit: This is what I already tried without success:
curl -XGET -G 'https://localhost/api/similar?' \
-d word=science \
-d target=en \
-d target=fr \
-d target=bg \
-d target=ar \
-d target=fr \
-d source=en \
-d limit=10 \
-d score=0.5
Unfortunately, this is not working
you're very close :) just replace -XGET with --get and it should work
curl --get 'https://localhost/api/similar?' \
-d word=science \
-d target=en \
-d target=fr \
-d target=bg
also note that the \ syntax for multi-line shell invocations is specific to posix shells, meaning it won't work on Microsoft Windows, in Windows's cmd it would be
curl --get 'https://localhost/api/similar?' ^
-d word=science ^
-d target=en ^
-d target=fr ^
-d target=bg
(and again ^ is exclusive to Windows so that won't work on posix shells~)

How Do You Pass a Variable to Curl in Bash Script

How Do You Pass a Variable to Curl in Bash Script
I'm not able to get this curl to work in a bash script using an environment variable. The API key gets misinterpreted somehow when passed in via a variable and I'm getting authentication errors on submit. If I plug in the API key with no surrounding quotes as plaintext, this works just fine. I've tried various forms of escaping quotes and other combinations. Any help would be great.
Code
#!/bin/bash
source .env
curl -X POST https://api.easypost.com/v2/shipments \
-u "$EASYPOST_TEST_API_KEY": \
-d 'shipment[to_address][name]=Dr. Steve Brule' \
-d 'shipment[to_address][street1]=179 N Harbor Dr' \
-d 'shipment[to_address][city]=Redondo Beach' \
-d 'shipment[to_address][state]=CA' \
-d 'shipment[to_address][zip]=90277' \
-d 'shipment[to_address][country]=US' \
-d 'shipment[to_address][phone]=8573875756' \
-d 'shipment[to_address][email]=dr_steve_brule#gmail.com' \
-d 'shipment[from_address][name]=EasyPost' \
-d 'shipment[from_address][street1]=417 Montgomery Street' \
-d 'shipment[from_address][street2]=5th Floor' \
-d 'shipment[from_address][city]=San Francisco' \
-d 'shipment[from_address][state]=CA' \
-d 'shipment[from_address][zip]=94104' \
-d 'shipment[from_address][country]=US' \
-d 'shipment[from_address][phone]=4153334445' \
-d 'shipment[from_address][email]=support#easypost.com' \
-d 'shipment[parcel][length]=20.2' \
-d 'shipment[parcel][width]=10.9' \
-d 'shipment[parcel][height]=5' \
-d 'shipment[parcel][weight]=65.9' \
Per Gordon Davisson's suggestion to break it down into a minimal request, it started sending right. Then I rebuilt it just as in my question above and for whatever reason it worked like a charm. The -x flag was also incredibly helpful in troubleshooting this but come to find out there wasn't anything inherently wrong with it in the first place. Thanks for everyone's responses!

Curl changes multipart/form-data path parameter

I try to send some multipart/form-data data using curl in a msys shell to a NAS named Synology. The form-data needs a parameter named "path" and must formated like "/dir/dir2". The slashes can't be changed.
My problem is, when i am using curl the path variable will be changed to "C:/git-sdk-64/dir/dir2" and i don't know how to prevent it. My command looks like this:
curl -X POST \
'http://url:port/webapi/entry.cgi?_sid=secret&api=SYNO.FileStation.Upload&method=upload&version=2' \
-F "path=/dir/dir2" \
-F 'overwrite=true' \
-F 'filename=#/c/Temp/test.txt'
Thanks to Daniel Stenberg's info i found out this is a "problem" with msys self. Msys fills up the path variable. Written down here http://www.mingw.org/wiki/Posix_path_conversion. The solution is to put an semicolon at the end of the path. The complete command now looks like this:
curl -X POST \
'http://url:port/webapi/entry.cgi?_sid=secret&api=SYNO.FileStation.Upload&method=upload&version=2' \
-F "path=/dir/dir2;" \
-F 'overwrite=true' \
-F 'filename=#/c/Temp/test.txt'

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= ....

cURL: (6) Couldn't resolve host 'GET' BASH script

I am having a problem with my bash script. It is producing an error of
curl (6) couldn't resolve host
What have I done wrong?
The following is my bash script.
#!/bin/bash
(set -o igncr) 2>/dev/null && set -o igncr; # this comment is needed
CookieFileName=cookies.txt
TEST="curl -k --cookie $CookieFileName --cookie-jar $CookieFileName POST -F "passUID=xxx&passUCD=xxx" https://wp1.coned.com/retailaccess/default.asp"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE
Try this instead :
#!/bin/bash
set -o igncr
CookieFileName='cookies.txt'
curl -k \
--cookie "$CookieFileName" \
--cookie-jar "$CookieFileName" \
--data "passUID=xxx&passUCD=xxx" \
"https://wp1.coned.com/retailaccess/default.asp" # POST request
If you need to load another page after that, simply chains cURL commands with the previous line :
curl -k \
--cookie "$CookieFileName" \
--cookie-jar "$CookieFileName" \
"https://wp1.coned.com/retailaccess/another_page.asp" # GET request
Note
Command Substitution: The $(foo bar) causes the command 'foo' to be executed with the argument 'bar' and $(..) will be replaced by the output. See http://mywiki.wooledge.org/BashFAQ/002, http://mywiki.wooledge.org/CommandSubstitution, and http://mywiki.wooledge.org/BashFAQ/082

Resources