curl: how to return 0 if status is 200?

curl --fail if not 200
curl error handling bash
curl get status code and body
curl grep status code
curl command to check url status
curl output http status info
curl see response status code
curl output just status code

How can I return 0 when the response status is 200?

Right now I'm able to get the status, e.g. 200 with the following command:

curl -LI -o /dev/null -w '%{http_code}\n' -s

But what I need to do is turn this 200 in a return 0.

How can I achieve this? I tried the following command but it dowsn't return

if [$(curl -LI -o /dev/null -w '%{http_code}\n' -s) == "200"]; then echo 0

Looks like you need some spaces and a fi. This works for me:

if [ $(curl -LI -o /dev/null -w '%{http_code}\n' -s) == "200" ]; then echo 0; fi

Can I make cURL fail with an exitCode different than 0 if the HTTP , --fail returns an exit code > 0 when the request fails 3 curl options: curl -s --fail --show-error > /dev/null curl -s --fail --show-error  A lot of effort has gone into the project to make curl return a usable exit code when something goes wrong and it will always return 0 (zero) when the operation went as planned. If you write a shell script or batch file that invokes curl, you can always check the return code to detect problems in the invoked command.

You can also use the -f parameter:

(HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts.


curl -f -LI

Will return status 0 if the call was sucessful.

How to Check HTTP Status Codes with cURL, supports HTTPS and performs SSL certificate verification by default when a secure protocol is specified such as HTTPS. You can make curl return actual HTTP status codes on standard out as long as you use the. -w <format> or --write-out <format>. command line option, using the format of %{http_code}.

Another way is to use the boolean operator && :

[ $(curl -LI -o /dev/null -w '%{http_code}\n' -s) == "200" ] && echo 0

The second command will be executed only if the first part is True.

curl hide progress bar output on Linux/Unix shell scripts, A lot of effort has gone into the project to make curl return a usable exit code when something goes wrong and it will always return 0 (zero) when the operation​  Wait for an HTTP endpoint to return 200 OK with Bash and curl -

The most simple way is to check for curl's exit code.

$ curl --fail -LI -o /dev/null -w '%{http_code}\n' -s > /dev/null
$ echo $?
$ curl --fail -LI -o /dev/null -w '%{http_code}\n' -s > /dev/null
$ echo $?

Please note that --fail is neccessary here (details in this answer). Also note as pointed out by Bob in the comments (see footnote) that in case of a non-200 success code this will still return 0.

If you don't want to use that for whatever reason, here's the other approach:

http_code=$(curl -LI -o /dev/null -w '%{http_code}\n' -s)
if [ ${http_code} -eq 200 ]; then
    echo 0

The reason your code isn't working is because you have to add spaces within the brackets.

(Copied from my answer on SuperUser where OP cross-posted the by now deleted question)

cURL, You can use the -w parameter to define the format curl outputs. To get the status code and nothing else, use something like this: $ curl -s -o  In this article, we will be calling the GET and POST endpoints using Curl. Please clone the NodeJS code into your local from this github repo. The repo has instructions on how to clone and run the NodeJS code. After cloning the code, go into the project folder and start the application using the following command.

Exit status, For more details, see How can I store the return value and/or output of a command web server, it displays the error document and exits with a status of 0 for success. In scripts it's better to have curl exit with unsuccessful status if there was a  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. PHP cURL HTTP CODE return 0. cURL call return

Return only a HTTP status code from curl command, Need to adjust shell or curl flags to fail hard if HTTP request isn't a 200. https://​​than-0-if-the-http-status-code-i return the header's status code  This runs curl in silent mode, follows any redirects, pulls the HTTP headers, and then pipes them to grep. grep will print the HTTP status code to standard output. HTTP Status Codes. Here is a link to a detailed reference of HTTP status codes. They boil down to this concise summary by @stevelosh: HTTP Status Code Summary (via @stevelosh) 1xx

Return code for curl used in a command substitution, apiRequest($cURL,$url['credentials']); if ($status == 200) { return true; } } return items and returns one of three values (−1, 0, or 1) to indicate whether the first  I'm using CURL to get the status of a site, if it's up/down or redirecting to another site. I want to get it as streamlined as possible, but it's not working well.