Curl suppress output when supplying header - bash

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/

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

"curl" does not work in script

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.

Get curl http code

I have the following cURL request. I want to get the http_code, but I want in a different variable, because otherwise it messes with parsing the JSON response from the GET call.
Is there anyway to do this?
curl --write-out %{http_code} --silent --output GET --header "Accept: application/json" --header "URL"
Just use command substitution to store status code in a variable:
status=$(curl --write-out %{http_code} --silent --output tmp.out GET --header "Accept: application/json" --header "URL")
data=$(<tmp.out)
# check status now
declare -p status
# check data
declare -p data
curl -i -H "Accept: application/json" "server:5050/a/c/getName{"param0":"Arvind "}"
curl -w 'RESP_CODE:%{response_code}' -s -X POST --data '{"asda":"asd"}' http://example.com --header "Content-Type:application/json"|grep -o 'RESP_CODE:[1-4][0-9][0-9]'
response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource") For response just try $response
Try this following may be this could help you to find the solution https://gist.github.com/sgykfjsm/1dd9a8eee1f70a7068c9

Bash script string expansion

I am having an issue with the entering the following command in bash at the moment:
RESPONSE=`curl -k "$line" --cert=\"$certfile:$certpassfile\" --silent --write-out --head '%{http_code}\n'`
where $line is the url, $certfile is the path to the pem file, $certpassfile is the certificate password.
I am getting the following error:
++ curl -k url '--cert="/certpath:certpassword"' --silent --write-out --head '%{http_code}\n'
curl: option --cert="/certpath:certpassword": is unknown
When i don't double the quotation marks around the certificate file and don't escape it, so the command looks like the below:
RESPONSE=`curl -k "$line" --cert="$certfile:$certpassfile" --silent --write-out --head '%{http_code}\n'`
I receive the same error but a different path:
++ curl -k url --cert=/certpath:certpassword --silent --write-out --head '%{http_code}\n' curl: option --cert=/certpath:certpassword: is unknown
Any idea how I can create the command at it should be which is:
curl -k url --cert="/certpath:certpassword" --silent --write-out --head '%{http_code}\n'
I think you just should strip that equal sign between --cert and the value:
RESPONSE=$(curl -k "$line" --cert "$certfile:$certpassfile" \
--silent --write-out --head '%{http_code}\n')

Resources