How Do You Pass a Variable to Curl in Bash Script - bash

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!

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~)

Curl command for conduit API

I've been trying to invoke conduit api using curl to add member to specific project and wrote following command:
curl https://test-caxzj6a226zp.phacility.com/api/project.edit -d api.token=api-[token] -d transactions[0][type]=parent -d transactions[0][value]=[project-phid] -d transactions[0][type]=members.add -d transactions[0][value][]=[user-phid]
I've followed the instructions on https://secure.phabricator.com/conduit/method/project.edit/ and was able to query project.search and user.search. But project.edit is giving me trouble. Any help will be appreciated.
PS: I get the following error: {"result":null,"error_code":"ERR-CONDUIT-CORE","error_info":"Validation errors:\n - Projects must have a name."}
You have a few errors in your call. First one is that you're specifying two transactions to apply within the same index. Formatting your call a bit, we get:
curl https://test-caxzj6a226zp.phacility.com/api/project.edit \
-d api.token=api-[token] \
-d transactions[0][type]=parent \
-d transactions[0][value]=[project-phid] \
-d transactions[0][type]=members.add \
-d transactions[0][value][]=[user-phid]
You have two definitions for transactions[0], if you want to apply multiple transformations, you need to increment your index:
curl https://test-caxzj6a226zp.phacility.com/api/project.edit \
-d api.token=api-[token] \
-d transactions[0][type]=parent \
-d transactions[0][value]=[project-phid] \
-d transactions[1][type]=members.add \
-d transactions[1][value][]=[user-phid]
Now, you didn't actually mention wanting to change the parent project, so I'm going to remove that transaction.
curl https://test-caxzj6a226zp.phacility.com/api/project.edit \
-d api.token=api-[token] \
-d transactions[0][type]=members.add \
-d transactions[0][value][]=[user-phid]
The users to add is also an array, so you need to specify an index for that argument:
curl https://test-caxzj6a226zp.phacility.com/api/project.edit \
-d api.token=api-[token] \
-d transactions[0][type]=members.add \
-d transactions[0][value][0]=[user-phid]
You could add additional users using -d transactions[0][value][1]=[user-phid], -d transactions[0][value][2]=[user-phid] etc.
Finally, you also need to specify which project you want to add the member to, since it's a required field. To do this, specify the project's numeric id (you can find this in its URL, /project/view/1/), its PHID, or its name/slug.
curl https://test-caxzj6a226zp.phacility.com/api/project.edit \
-d api.token=api-[token] \
-d transactions[0][type]=members.add \
-d transactions[0][value][0]=[user-phid] \
-d objectIdentifier=[project-phid]
You can actually submit a test call to conduit from within the conduit application, and after submitting it, one of the tabs in the example section will display the curl call required to perform that action via the API:
https://secure.phabricator.com/conduit/method/project.edit/

Sending cURL data to multiple URLs

My code consists of the following:
curl -k -X DELETE \
-H "X-Parse-Application-Id: staticid" \
-H "X-Parse-Master-Key: statickey" \
https://mystatic.url/dynamicvalue
I have a list of URLS:
https://mystatic.url/johndylan
https://mystatic.url/marypoppins
etc, included in a tobedeleted.txt file and I would like to modify my cURL code to be something like this (which I've tried but didnt work:
curl -k -X DELETE \
-H "X-Parse-Application-Id: staticid" \
-H "X-Parse-Master-Key: statickey" \
> tobedeleted.txt
or to something like this (which also I've tried but it didnt worked)
curl -k -X DELETE \
-H "X-Parse-Application-Id: staticid" \
-H "X-Parse-Master-Key: statickey" \
https://mystatic.url/$tobedeleted.txt
Note that i want to run the same cURL command, each time for each line of the file, so I guess that I would need something like foreach function, since this is a bash script.
You are going to want something like this:
while read value
do
curl -k -X DELETE \
-H "X-Parse-Application-Id: staticid" \
-H "X-Parse-Master-Key: statickey" \
https://mystatic.url/$value
done < tobedeleted.txt

curl call with parameters into variable doesn't work

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

Entering Password Automatically for Stripe From Bash Script

I am using Stripe's Curl API to create subscriptions.
My bash script looks like this
#!/bin/bash
set pass MyPasword123
curl https://api.stripe.com/v1/customers -u sk_test_BQokikJOvBiI2HlWgH4olfQ2 -d card[number]=4242424242424242 -d card[exp_month]=12 -d card[exp_year]=2016 -d card[cvc]=123 -d plan=EarlyAdopter -d email=test#gmail.com
expect -re "Enter host password for user 'sk_test_BQokikJOvBiI2HlWgH4olfQ2:':"
send "${pass}\r"
This does now work.
How do I enter automatically a password for the Stripe Curl API?
Thanks...
The issue here is that you are only passing the API key but you're missing the : at the end. This is mentioned in the documentation about the Authentication. It should be like this:
curl https://api.stripe.com/v1/customers \
-u sk_test_XXX: \
-d card[number]=4242424242424242 \
-d card[exp_month]=12 \
-d card[exp_year]=2016 \
-d card[cvc]=123 \
-d plan=EarlyAdopter \
-d email=test#gmail.com

Resources