How to include an '&' character in a bash curl statement - bash

I'm trying to use curl in bash to download a webpage, but the & symbol in the URL isn't interpreted as a character as I would like. Any ideas on how I can convince bash that the symbol & is just a boring character and nothing special?

Putting the entire URL inside double quotes should take care of your problem.

curl "http://www.example.com?m=method&args=1"
Are you using the & as a delimiter for a GET URL? Or is in a piece of data?
If it is in data you must encode it to an HTML character, if not, surround with quotes.
The encoding for & should become %26 in the URL.
curl "http://www.example.com?m=this%26that

Putting single quotes around the & symbol seems to work. That is, using a URL like http://www.example.com/page.asp?arg1=${i}'&'arg2=${j} with curl returns the requested webpage.

Instead of trying the escape "&" characters, you can specify http url parameters in POST requests with -d parameter as shown below:
curl -X POST http://www.example.com \
-d arg1=this \
-d arg2=that
If the request is a GET, then you should also add -G option, which tells curl to send the data with GET request.
curl -X GET -G http://www.example.com \
-d arg1=this \
-d arg2=that

You can use %26 instead &.
The entity code will work fine.

Use the parameter --url with double quotes worked for me

easiest way :
curl -X GET -G http://example.com -d var1=$1 -d var2=$2

Related

When to use ANSI-C Quoting in shell?

I am writing a simple library that generates curl command for a given HTTP request. I want the generated curl command to be as legible for a user as possible but also properly encoded.
In other word, should I use ANSI-C quoting and generate
curl 'http://server' -d $'name=Administra\xe7\xe3o'
or
curl 'http://server' -d 'name=AdministraĆ§Ć£o'
?

Passing string to curl without literal double quotes

I am trying to write a shell script that invokes two APIs using curl.
One of the keys of JSON output of the first curl is passed to the second curl. In the Bash script below, I am passing token as a command line parameter to the first curl and it works fine.
The output of the first curl is extracted into client_token and I am passing it to the second curl. It is failing.
The reason being, wherever I have $client_token, the value is getting substituted as "value" (with quotes) instead of value (without quotes). Curl expects strings without quotes in the second curl. How can I get rid of double quotes?
echo $1
XVaultToken=`curl -X POST "https://sub.domain.tld:8200/login" -d '{"token":"'"$1"'"}'`
client_token=`echo $XVaultToken|jq '.auth.client_token'
echo $client_token
apiKey=`curl -X GET https://sub.domain.tld:8200/api-key -H 'X-Vault-Token: "'"$client_token"'"'`
#apiKey=`curl -X GET https://sub.domain.tld:8200/api-key -H 'X-Vault-Token: $client_token'`
echo "apikey"
Probably your jq command is outputting the quotes that you don't want. Ask jq for the raw value instead:
client_token=`echo $XVaultToken|jq -r '.auth.client_token'

Variable expansion inside single quotes

I am quite new to shell scripting.
I have the following script:
out="FAILURE"
curl -X POST -d 'json={"json":"message"}' http://localhost:8888/json.tail.test
I want to replace "message" with the $out's value. I tried different ways but could not get that done. Can someone please suggest me?
Do this:
out="FAILURE"
curl -X POST -d 'json={"json":"'$out'"}' http://localhost:8888/json.tail.test
Basically, enclose everything except $out inside single quotes. Single quotes protect double quotes but suppress the expansion of variables like $out.
Try this:
out="FAILURE" curl -X POST -d 'json={"json": $OUT}' http://localhost:8888/json.tail.test
You just need to literally replace "message" with $OUT

variable in bash not working [duplicate]

This question already has answers here:
How do I use variables in single quoted strings?
(8 answers)
Closed 6 years ago.
I have a simple function in my bashrc file, it takes 2 arguments and makes a curl call. The problem is, the curl request is not getting the variable. Here's the function...
checkUserName() {
cd ~/
echo "checking for username $2"
curl -w "#curl-format.txt" -H "Content-Type: application/json" -X POST -d '{"userName": "$2"}' http://localhost:8080/$1/verify
}
alias unameCheck=checkUserName
Then I call it with something like...
unameCheck users danwguy
and I will see...
checking for username danwguy
in my terminal prompt, however when I look at my logs from my application it shows that it is checking for userName $2
So the variable isn't being replaced in the curl command, even though the $1 is being replaced, since it is calling the correct API on my localhost.
It replaces it in the echo command, but not in the curl command.
I have even tried creating a local variable, but that still doesn't work, no matter what I do, it doesn't replace in the curl call, but it does in the echo call.
Can anyone see why it wouldn't be properly replacing the $2
Parameter expansions ($var) will not expand in single quotes. Use double quotes instead:
$ curl -w "#curl-format.txt" \
-H "Content-Type: application/json" \
-X POST \
-d '{"userName": "'"$2"'"}' \
"http://localhost:8080/$1/verify"
Also wrap parameter expansions in double quotes to avoid word splitting and
pathname expansion.
Just a note to previous comment.
You can escape double quotes with backslash, to tell bash interpreter to not interpret its special meaning, so final code looks like:
... -d "{\"userName\": \"$2\"}" ...
Which is way more obvious for me...

How to send double quote in -d parameter for curl.exe?

How can I send a double quote char using curl.exe in the -d parameter. I don't want to URL encode the double quote. Since the -d data needs to be surrounded in double quotes, I cannot seem to get it to work.
Or is there another flag for curl.exe that tells it to use a files contents for the whole form post data?
My curl.exe works with this form:
-d "{\"param\":\"value\"}"
i.e. doublequotes around data, and doublequotes masked with backslash inside
You can most certainly escape double quotes. How you do that depends on your operating system and shell, which you fail to specify. On Windows, you'd use the ^ as the escape character.
You can also do this:
curl [...] -d #filename
...which reads post data from a file called filename.
Google and/or man is your friend.
http://curl.haxx.se/docs/manpage.html
For the escaping of double quote question, I'm finding that tripling the doublequotes works from the shell:
curl -d {"""foo""":"""bar"""}
while doubling the doublequotes works from within a batch file:
curl -d {""foo"":""bar""}
Which is quite annoying for testing in the shell first.
You can surround the data with single quotes and use double quotes inside.
Example in PowerShell
curl.exe https://httpbin.org/anything `
-H 'Content-Type: application/json' `
-d '{ "this": "is proper json" }'.Replace('"', '""')
Please note that cURL is built into Windows 10, but PowerShell shadows it with an alias, so you have to use curl.exe
If you are doing this in Powershell, you will need to quote the whole text block with single quotes and still escape the quotes. i.e. -d '{\"param\":\"value\"}'
Tested on Win11 VSCode Powershell 7 terminal
There is something called dollar slashy string.
def cmd = $/ curl -d '{"param":"value"}' -X POST https://example.com/service /$
sh cmd

Resources