Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 85Web · HTTP

How to test an HTTP endpoint with curl

"Is the API up?", "Why is it slow?", "Does the new server answer before we switch DNS?" — curl answers from any terminal, in scripts and in CI. This how-to shows the status code and timing in one line, headers and redirects, JSON POSTs with a token, testing a server before DNS points to it, and a time breakdown that shows where the seconds go (DNS, connect, TLS, server).

  • 5 Lessen
  • 6 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

85.1

The quick answer

curl -sS -o /dev/null -w '%{http_code} %{time_total}s\n' https://shop.example.com/healthz
200 0.084s
curl -sSI https://shop.example.com
curl -sS -X POST https://api.example.com/v1/orders -H 'Content-Type: application/json' -H "Authorization: Bearer $TOKEN" -d '{"sku":"A-100","qty":2}'

-s silent, -S but show errors, -o /dev/null hide the body, -w print what you want.

Commando's in deze les

85.2

Step by step

  1. Status and time (quick answer) — the fastest health check.
  2. Headers and redirects:
curl -sSI http://shop.example.com
HTTP/1.1 301 Moved Permanently
Location: https://shop.example.com/
curl -sSIL http://shop.example.com | grep -E '^HTTP|^location'

-I = headers only (HEAD request), -L = follow redirects.

  1. See everything (TLS, request and response headers) when something is wrong:
curl -sv https://shop.example.com -o /dev/null 2>&1 | grep -E '^\*|^>|^<' | head -40
  1. Where does the time go? A timing breakdown:
curl -sS -o /dev/null -w 'dns:%{time_namelookup} connect:%{time_connect} tls:%{time_appconnect} first-byte:%{time_starttransfer} total:%{time_total}\n' https://shop.example.com/
dns:0.012 connect:0.031 tls:0.074 first-byte:0.912 total:0.915

Big first-byte minus tls = the server/app is slow; big dns or connect = network/DNS.

  1. Test a new server before DNS switches — send the right name to a specific IP:
curl -sSI --resolve shop.example.com:443:203.0.113.20 https://shop.example.com/

TLS and virtual hosts work as if DNS already pointed there.

  1. APIs: JSON body, token, pretty output, and fail on HTTP errors in scripts:
curl -sS --fail-with-body -H "Authorization: Bearer $TOKEN" https://api.example.com/v1/orders/42 | jq .
curl -sS --retry 3 --retry-delay 2 --max-time 10 https://api.example.com/healthz

--fail-with-body = exit code 22 on 4xx/5xx (CI stops) and still prints the error body.

  1. Only for tests: -k skips TLS checks. Better: --cacert ca.crt for an internal CA.

85.3

Check that it worked

You know the status code, the time (and which part is slow), and the headers — and the same command in a script returns a non-zero exit code when the endpoint is broken:

curl -sS --fail -o /dev/null https://shop.example.com/healthz && echo OK || echo FAIL

85.4

Common problems

ProblemCauseFix
curl: (6) Could not resolve hostDNS.dig NAME; test with --resolve.
curl: (7) Failed to connectNothing listens, or a firewall.Test the port: nc -zv HOST 443.
curl: (28) Operation timed outFirewall drops, or the server hangs.--max-time; check from another network; server logs.
curl: (60) SSL certificate problemUnknown CA, expired or wrong-name certificate.curl -v; fix the cert or use --cacert.
401 / 403Missing or wrong token, or no right.Check the header; the token's scopes.
Token visible in shell historyTyped on the command line.Use a variable from a file: TOKEN=$(cat token.txt), or -H @headers.txt.

85.5

Go further

1How do you test a new server for shop.example.com before DNS points to it?
2In the timing output, first-byte is 0.9 s but tls is 0.07 s. Where is the time lost?

In the server or app (it needs about 0.8 s to start answering).

Alle commando's in dit hoofdstuk