Dimitri Kopriwa
Dimitri Kopriwa

Reputation: 14363

How to test and wait for an HTTP service using cURL?

Description

I have started up a docker HTTP service

I want to wait the HTTP service to be UP and running using curl

Reproduction

I have written the following bash script

#!/bin/bash

found_message_in_a_bottle() {
  curl -H "Accept: application/json" --connect-timeout 2 -s "$1" 2>/dev/null| grep -q Welcome
}

wait_for_saltmaster() {
  while ! found_message_in_a_bottle "$1"; do
    echo wait for service... 
    sleep 1
  done
}

I then test my service :

source myscript.sh
wait_for_saltmaster localhost:8080

Expected

I expect to wait for service until I have 200 code and HTTP response.

Result

For some services, I have HTTP service 200 code but the curl request keep retrying.

Question

Is there anything wrong in my cURL command ?

Upvotes: 2

Views: 9687

Answers (1)

Ben
Ben

Reputation: 5129

If you are checking the HTTP 200 response code, you should try to lookup if the response header having the HTTP 200 response code.

In general the response header consists of these information.

HTTP/1.1 200 OK
Date: Mon, 11 Dec 2017 23:59:11 GMT
Server: Apache/2.2.32 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.1.8 mod_ssl/2.2.32 OpenSSL/1.0.2j DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.24.0
X-Powered-By: PHP/7.1.8
Content-Length: 0
Content-Type: text/html; charset=UTF-8 

You can check the response header, by this curl command, extract the first line and see if it is 200 OK.

curl -H "Accept: application/json" --connect-timeout 2 -s -D - "$1" -o /dev/null 2>/dev/null | head -n1 | grep 200

Upvotes: 3

Related Questions