curl --fail without suppressing stdout - bash

I have a script that uploads a file to a WebDav server using curl.
curl --anyauth --user user:password file http://webdav-server/destination/
I want two things at the same time:
have the script output to stdout (which is directed to a log file)
detect whether the upload succeeded
As far as I know, curl returns an exit code of 0 even in 401(unauthorized) or 407(conflict) situations. The --fail option can be used to change this behavior, but it suppresses stdout.
What would be the best workaround for this? tee and grep?

curl writes its output to stderr(2), the error stream instead of stdout(1). Redirect it on the command using 2>&1
curl --fail --anyauth --user user:password file http://webdav-server/destination/ 2>&1 > logFile
retval=$?
if [ $retval -eq 0 ]; then
echo "curl command succeeded and the log present in logFile"
fi

Related

curl 400 bad request (in bash script)

I trying to do execute the following script in bash
#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH
if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7
else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi
But keep receiving a 400 bad request from the server.
When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.
The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.
edit 1 - hidding some sensitive content
edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)
The command i neet to input in server is:
curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7
And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.
I'm a bit new to the curl, any help can be apreciate.
Sorry for the bad english and tks in advance.

How to retrieve error code from cURL on shell

I know a similar question was posted, but I can't get it to work on my machine.
I tried the 1st answer from the mentioned question, i.e. response=$(curl --write-out %{http_code} --silent --output /dev/null servername) and when I echo $response I got 000 [Not sure if that is the desired output].
However, when trying to do so with my cURL command, I get no output.
This is my command:
curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
and I use it with
x=$(curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt)
but when I try to echo $x all I get is a newline...
I know the cURL is failing, because when I run the same command, without --silent, I get curl: (7) Couldn't connect to server
This Q is tagged with both sh, bash because I've tried it on both with same results
I found this option which kind of helps (but I still don't know how to assign it to a variable, which should be easier than this...):
--stderr <file>
Redirect all writes to stderr to the specified file instead. If the file name is a plain '-', it is instead written to stdout.
If this option is used several times, the last one will be used.
When I use it like this:
curl -k --silent -S --stderr my_err_file --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
I can see the errors (i.e. curl: (7) Couldn't connect to server) inside that file.
I used --silent to suppress all output, and -S to un-suppress the errors, and the --stderr <file> to redirect them

Login via curl fails inside bash script, same curl succeeds on command line

I'm running this login via curl in my bash script. I want to make sure I can login before executing the rest of the script, where I actually log in and store the cookie in a cookie jar and then execute another curl in the API thousands of times. I don't want to run all that if I've failed to login.
Problem is, the basic login returns 401 when it runs inside the script. But when I run the exact same curl command on the command line, it returns 200!
basic_login_curl="curl -w %{http_code} -s -o /dev/null -X POST -d \"username=$username&password=$password\" $endpoint/login"
echo $basic_login_curl
outcome=`$basic_login_curl`
echo $outcome
if [ "$outcome" == "401" ]; then
echo "Failed login. Please try again."; exit 1;
fi
This outputs:
curl -w %{http_code} -s -o /dev/null -X POST -d "username=bdunn&password=xxxxxx" http://stage.mysite.it:9301/login
401
Failed login. Please try again.
Copied the output and ran it on the cmd line:
$ curl -w %{http_code} -s -o /dev/null -X POST -d "username=bdunn&password=xxxxxx" http://stage.mysite.it:9301/login
200$
Any ideas? LMK if there's more from the code you need to see.
ETA: Please note: The issue's not that it doesn't match 401, it's that running the same curl login command inside the script fails to authenticate, whereas it succeeds when I run it on the actual CL.
Most of the issues reside in how you are quoting/not quoting variables and the subshell execution. Setting up your command like the following is what I would recommend:
basic_login_curl=$(curl -w "%{http_code}" -s -o /dev/null -X POST -d "username=$username&password=$password" "$endpoint/login")
The rest basically involves quoting everything properly:
basic_login_curl=$(curl -w "%{http_code}" -s -o /dev/null -X POST -d "username=$username&password=$password" "$endpoint/login")
# echo "$basic_login_curl" # not needed since what follows repeats it.
outcome="$basic_login_curl"
echo "$outcome"
if [ "$outcome" = "401" ]; then
echo "Failed login. Please try again."; exit 1;
fi
Running the script through shellcheck.net can be helpful in resolving issues like this as well.

Bash script and curl: how to get status code and content and store in var in same command

In PHP or Python, for example, you cand do curl and receive a object with all properties organized.
In bash I have to invoke curl application.
I need to get status code and content in same command (its obvious because the status code is relationed with success or not to get content). But how I can do this?
I am trying this:
# get url in function
urlcurl=$(echo $#)
# create new file descriptor and redirect to STDOUT
exec 3>&1
# get curl status code and store in curlstatuscode
curlstatuscode=$(curl -L -k -w "%{http_code}" -o >(cat >&3) --silent \"${urlcurl}\")
My problem is about content: When I execute this in terminal I receive content in STDOUT (that is the command had to do). But I am trying to store this STDOUT using regular redirect expressions and they not work.
One example:
exec 3>&1
HTTP_STATUS=$(curl -k --silent -L -w "%{http_code}" -o >(cat >&3) 'http://example.com')
echo $HTTP_STATUS
If you not understood what I am trying to do, roughly mode (and invalid) would be something like that:
(I know that is invalid, I only want to clarify)
HTTP_CONTENT=$(echo `HTTP_STATUS=$(curl -k --silent -L -w "%{http_code}" -o >(cat >&3) 'http://example.com'`)
HTTP_CONTENT will get get content and HTTP_STATUS will get curl status code.
Please do not say to user another language. I need to solve this in bash. Is very simple to do that in other languages (mainly oriented objects). I really want to do this in bash.
Thank you!
Since http status is the last line in curl output, you can do like this in BASH:
out=$(curl -k --silent -L -w "\n%{http_code}" 'minecraft.net/haspaid.jsp?user=apterixbr')
http_status="${out##*$'\n'}"
http_content="${out%$'\n'*}"

I need to execute a Curl script on Jenkins so as to check the status URL

The script should check for Http status code for the URL and should show error when status code doesn't match for eg. 200.
In Jenkins if this script fails then Build should get failed and Mail is triggered through post build Procedure.
Another interesting feature of curl is its -f/--fail option. If set, it will tell curl to fail on any HTTP error, i.e. curl will have an exit code different from 0, if the server response status code was not 1xx/2xx/3xx, i.e. if it was 4xx or above, so
curl --silent --fail "http://www.example.org/" >/dev/null
or (equivalently):
curl -sf "http://www.example.org/" >/dev/null
would have an exit code of 22 rather than 0, if the URL could not be found or if some other HTTP error occurred. See man curl for a description of curl's various exit codes.
You can use simple shell command as referred in this answer
curl -s -o /dev/null -w "%{http_code}" http://www.example.org/
This will happen if the following shell script is added:
response=$(curl -s -o /dev/null -w "%{http_code}\n" http://www.example.org/)
if [ "$response" != "200" ]
then
exit 1
fi
exit 1 will mark build as failed
Jenkins also has HTTP Request Plugin that can trigger HTTP requests.
For example this is how you can check response status and content:
def response = httpRequest "http://httpbin.org/response-headers?param1=${param1}"
println('Status: '+response.status)
println('Response: '+response.content)
You could try:
response=`curl -k -s -X GET --url "<url_of_the_request>"`
echo "${response}"
How about passing the URL at run time using curl in bashscript
URL=www.google.com
"curl --location --request GET URL"
How we can pass url at runtime ?

Resources