"curl" does not work in script - bash

I wrote curl which returns only http status code :
curl --write-out %{http_code} \n
--silent \
--output /dev/null \
$URL
It works fine if I execute this from console. But after I have puted it into script, like this:
HTTP_STATUS=$(curl --write-out %{http_code} \n
--silent \
--output /dev/null \
$URL)
And try to echo $HTTP_STATUS, result is 200000000000000000000000000000000000000000000000000
How can I fix it?

I wrote curl which returns only http status code
There are couple of issues with your script.
Your use of UPPERCASE variables might override shell environment variables.
The --write-out argument of the curl can ideally be within double quotes.
status=$(curl --write-out "%{http_code}" --silent --output /dev/null "$url")
echo "$status" # Would give you just the status
Note: As pointed out in this comment you don't need the newline too since you're assigning the value to a variable.

Related

What is syntax for single quote inclusion when defining curl --noproxy in bash script?

I want to include --noproxy '*' with curl in a CURL definition in a bash script, i.e.
CURL='curl --noproxy \'*' --fail --max-time 10 --silent --output /dev/null --write-out '%{http_code}\\n''
But this does not correctly process '*'.
echo $CURL
curl --noproxy \* --fail --max-time 10 --silent --output /dev/null --write-out %{http_code}\n
Can anyone advise as to the correct syntax for inclusion of the single quotes with *, which noproxy requires?
If you really want to store the command somewhere, before executing it, don' use a scalar, but an array:
CURL=(curl --noproxy '*' --fail --max-time 10 --silent --output /dev/null --write-out '%{http_code}\\n')
and execute it with
"${CURL[#]}"
This not only makes quoting easier, but also works if one of the arguments contains a space.
You would need to escape both 's, but curl doesn't need the quotes. The quotes are simply to prevent the shell from expanding * before curl sees it. '*' and \* are equivalent.
Don't define a parameter to execute as a command; define a function.
CURL () {
curl --noproxy '*' --fail --max-time 10 --silent --output /dev/null --write-out '%{http_code}\n' "$#"
}

Curl suppress output when supplying header

This suppresses the output and just outputs the status:
curl --write-out '%{http_code}' --silent --output /dev/null --noproxy '*' http://www.google.com/
Adding a header makes the entire response print:
curl --write-out '%{http_code}' --silent --output /dev/null --noproxy '*' --header ''"'"'Host:' '192.168.0.1:2345'"'" http://www.google.com/
How can I stop the output printing to stdout when injecting a header?
Let's take a closer look at your command. Below invocation will write each separate argument in a new line.
$ printf '%q\n' curl --write-out '%{http_code}' --silent --output /dev/null --noproxy '*' --header ''"'"'Host:' '192.168.0.1:2345'"'" http://www.google.com/
curl
--write-out
%\{http_code\}
--silent
--output
/dev/null
--noproxy
\*
--header
\'Host:
192.168.0.1:2345\'
http://www.google.com/
Bingo, though Host: 192.168.0.1:2345 must be a single argument, you're providing it to curl as two separate arguments, so it tries to fetch 192.168.0.1:2345' first. And since --output is applied to only one URL, the response from http://www.google.com/ is printed.
Do it like this and it'll work.
curl --write-out '%{http_code}\n' --silent --output /dev/null --noproxy '*' --header 'Host: 192.168.0.1:2345' http://www.google.com/

curl PUT using auth token header to mesosphere fails without eval

EDIT:
I have managed to make it work with
response=$(
curl -k -X PUT -d "$marathon_payload" --write-out %{http_code} --silent --output "$tmp"\
-H "Authorization: token=$dcos_token" -H "$header_content_type" $app_id_url
)
The single quotes were causing the problem. It took a few gyrations but all good.
MORAL: quotes inside the value don't matter if the value is properly quoted UNLESS you eval the whole thing, and I should have known that. Occam's wins again.
end edit
I am initiating Mesosphere microservice deployments with curl, but it won't succeed without using eval. Since I recently inherited this code I've been trying to scrub the eval out of it just as a matter of habit, but it's thwarting me.
The script initiates the deployment with
response=$(
eval curl -k -X PUT -d "'$marathon_payload'" --write-out %{http_code} --silent --output $tmp\
-H "'Authorization: token=$dcos_token'" -H "'$header_content_type'" $app_id_url
)
If it gets a 200 or a 201, it loops a curl to effectively screen-scrape the deployments page till the request disappears.
chkDeploy() { rm -f $tmp;
eval curl -k -X GET --silent --write-out %{http_code} --silent --output $tmp\
-H "'Authorization: token=$dcos_token'" -H "'$header_content_type'" $deployments_url
}
response=$( chkDeploy )
$dcos_token is a base64 encoded string.
It then checks the service with another curl loop to the info page so it can verify the version number. This one is working fine with no eval.
chkCode() {
curl -k -X GET --write-out %{http_code} --silent --output $tmp $info_url;
}
response=$( chkCode )
The first two return 401, authentication failure.
I'm guessing the auth token quoting is off.
There's no reason to use eval here; you just need to quote the arguments to -H properly.
response=$(
curl -k -X PUT -d "$marathon_payload" \
--write-out %{http_code} \
--silent --output "$tmp" \
-H "Authorization: token=$dcos_token" \
-H "$header_content_type" "$app_id_url"
)

Assign curl http reponse code to variable

I've got the following call in a bash script I'm creating
response= curl -X POST $URL -u "$USER:$PASSWORD" --data-urlencode "key=$key" --data "label=pub_key" -o /dev/null --silent --write-out "%{http_code}"
I can see 200 being written to the console however $response always ends up null.
I've also tried the following but it was not better.
response= $(curl -X POST $URL -u "$USER:$PASSWORD" --data-urlencode "key=$key" --data "label=pub_key" -o /dev/null --silent --write-out "%{http_code}")
Any help for a bash noob would be appreciated.
Space -- the final frontier:
response=$(curl -X ...)
Note: no spaces around the =. The shell is white-space sensitive in a few places and variable assignments are one of them.
With the space, as in var= command args, you set var as empty in a one-shot assignment and then run command.

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