Linux Script with Curl to Check Webservice Is Up

Linux script with curl to check webservice is up

curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
  • -s = Silent cURL's output
  • -L = Follow redirects
  • -w = Custom output format
  • -o = Redirects the HTML output to /dev/null

Example:

[~]$ curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
200

I would probably remove the \\n if I were to capture the output.

Linux script with curl to check webservice status

Check out this question:

https://superuser.com/questions/272265/getting-curl-to-output-http-status-code

You should be able tou se something like this (from the second answer there) in a script for your logic:

curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/

Remove the -I if your web service doesn't like HEAD requests.

Linux script with curl to check to check recursively if webservice is up

In respect to the comment about recursive functions in Bash, I've created a small test and which seems to work at a first glance.

./conTest.sh

#! /bin/bash

test_command()
{

http_code=$(curl -sL -x "http://localhost:3128" -w "%{http_code}\\n" "https://www.google.com/" -o /dev/null --connect-timeout 3 --max-time 5)

if [ ${http_code} == "200" ] ;
then
echo "OK" ;
else
echo "KO" ;
sleep 1 ;
test_command ;
fi

}

test_command ;

Maybe one can provide a more elegant solution or has an other idea.

How to consume webservice using CURL command?

The & character is special in the shell; so the command is executing fine, just being sent to the background. These numbers [1] 4373
[2] 4374
[3] 4375
are the process id's that are sent to the background. You can tell later on that they are finished with [1] Done. To avoid this, you should quote the URL.

You also need to supply the -o option to curl; because I assume you are trying to save the json file to be processed later:

curl -o bikes.json "http://maps.googleapis.com/maps/api/...."

You can also use wget which is designed especially for this:

wget "http://maps.googleapis.com/maps/api/...." -O bikes.json

Or, my personal favorite httpie:

http "http://maps.googleapis.com/maps/api..." > bikes.json

To parse json at the shell, you can use a tool like jsawk. However, I prefer the much more simpler:

curl -s "http://www.example.com/..." | python -mjson.tool | grep "distance"

How to test web service using command line curl

In addition to existing answers it is often desired to format the REST output (typically JSON and XML lacks indentation). Try this:

$ curl https://api.twitter.com/1/help/configuration.xml  | xmllint --format -
$ curl https://api.twitter.com/1/help/configuration.json | python -mjson.tool

Tested on Ubuntu 11.0.4/11.10.

Another issue is the desired content type. Twitter uses .xml/.json extension, but more idiomatic REST would require Accept header:

$ curl -H "Accept: application/json"

Accessing web service with curl, in a /bin/sh script, causes HTTP 415 Unsupported Media Type

For me it worked this way:

#!/bin/sh

read -d '' CMD << EOF
`/usr/bin/curl -s http://localhost:8080/someurl \
--header "Content-type: application/json"`
EOF

echo $CMD

What if evaluate it inside of EOF?

Curl command line for consuming webServices?

Posting a string:

curl -d "String to post" "http://www.example.com/target"

Posting the contents of a file:

curl -d @soap.xml "http://www.example.com/target"


Related Topics



Leave a reply



Submit