Using Environment Variables in cURL Command - Unix
Single quotes inhibit variable substitution, so use double quotes. The inner double quotes must then be escaped.
... -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}"
Since this answer was written in 2015, it has become clear that this technique is insufficient to properly create JSON:
$ USERNAME=person1
$ PASSWORD="some \"gnarly 'password"
$ echo "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}"
{"username":"person1","password":"some "gnarly 'password"}
$ echo "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}" | jq .
parse error: Invalid numeric literal at line 1, column 47
The quoting problem are clear. The (shell) solutions are not
Current best practice: use a JSON-specific tool to create JSON:
jq
$ jq -n -c --arg username "$USERNAME" --arg password "$PASSWORD" '$ARGS.named'
{"username":"person1","password":"some \"gnarly 'password"}jo
$ jo "username=$USERNAME" "password=$PASSWORD"
{"username":"person1","password":"some \"gnarly 'password"}
And with curl:
json=$( jq -n -c --arg username "$USERNAME" --arg password "$PASSWORD" '$ARGS.named' )
# or
json=$( jo "username=$USERNAME" "password=$PASSWORD" )
# then
curl ... -d "$json"
How can I set the environment variable in request header for curl?
Use --header "access_token: \"${SOME_ENV_TOKEN}\""
The '
quotes disable variable expansion in sh.
Passing certs to curl from an environment variable
Bash supports process substitution using the <(cmd)
syntax. This causes the output of cmd
to be substituted with a filename. This allows you to pass command output as an argument where a filename is expected.
In your example, you would do something like this:
CACERT=$(vault kv get -field crt my/cert/path/CACERT)
CERT=$(vault kv get -field crt my/cert/path/TESTCERT)
KEY=$(vault kv get -field key my/cert/path/TESTCERT)
curl -v \
--cacert <(echo "$CACERT") \
--cert <(echo "$CERT") \
--key <(echo "$KEY") \
--location \
--request GET 'https://my.end.point'
How to pass a variable in a curl command in shell scripting
When using variables in shell, you can only use doubles quotes, not single quotes : the variables inside single quotes are not expanded.
Learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words
Bash curl and variable in the middle of the url
It's not a quoting issue. The problem is that your keyword file is in DOS format -- that is, each line ends with carriage return & linefeed (\r\n) rather than just linefeed (\n). The carriage return is getting read into the line variable, and included in the URL. The giveaway is that when you echo it, it appears to print:
/subscriptions?v=2&alt=jsoneeds/api/users/KEYWORD FROM FILE"
but it's really printing:
https://gdata.youtube.com/feeds/api/users/KEYWORD FROM FILE
/subscriptions?v=2&alt=json
...with just a carriage return between them, so the second overwrites the first.
So what can you do about it? Here's a fairly easy way to trim the cr at the end of the line:
cr=$'\r'
while read line
do
line="${line%$cr}"
curl "https://gdata.youtube.com/feeds/api/users/${line}/subscriptions?v=2&alt=json" \
> "/home/user/archive/$line"
done < textfile.txt
Passing Bash Variable to CURL
It seems there are multiple issues.
1) To address the question asked by your title, when you use $post as an argument, the white space in its value causes it to be treated as multiple arguments by Bash. Try putting quotes around it so that it's treated as one argument.
2) I'm guessing you added single quotes to $post, in an attempt to have Bash treat it as a single parameter. Try removing the single quotes.
3) For me at least, all of the backslashes in the curl
command were causing it to fail. Maybe you had it split across multiple lines and the copy/paste didn't translate that. I've removed them in my example below.
Putting all together:
post="{\"Name\" : \"Vikram\"}"
curl --silent --insecure -X POST -d "${post}" -H "Content-Type: application/json" $restUrl
insert multiple environment variables in curl json
I actually did store all the variables to a single variable and used in the curl command which worked
TZ=EST5EDT date
Hello=XYZ
Update="$TZ","$Hello"
curl -X POST --data-urlencode 'payload={"XXXXX": "#XXXXXX", "username": "test for automation", "text":"'"$update"'","icon_emoji": ":ghost:"}' https://url
Related Topics
The Address Where Filename Has Been Loaded Is Missing [Gdb]
Is It Safe to Issue Blocking Write() Calls on the Same Tcp Socket from Multiple Threads
What Is the Best Git Config Set Up When You Are Using Linux and Windows
Mpi_Send Takes Huge Part of Virtual Memory
Why Gcc 4.X Default Reserve 8 Bytes for Stack on Linux When Calling a Method
Using --Http2.0 Option with Curl 7.33.0 Gives Unsupported Protocol
Cannot Install Extensions in Visual Studio Code
How to Close a Non-Blocking Socket
Parse CSV in Bash and Assign Variables
Curl Progress Bar: How to Pipe and Extract Numbers Only Using Grep
Amazon Ec2 Mail Limit Reached While Using External Smtp Server to Send Emails
How to Speed Up Compilation Time in Linux
Difference Between Real User Id, Effective User Id and Saved User Id
How to Change the Default Gcc Compiler in Ubuntu
Why Should I Recompile an Entire Program Just for a Library Update
How to Get Eclipse Swt Browser Component Running on Ubuntu 11.04 (Natty Narwhal) with Webkit