Tips for Using Curl as a REST Client

> docker run -it --rm centos:8 curl --version

Basic Curl Downloads to File or STDOUT

curl -O http://somewebsite.com/path/payload.tar
curl https://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespeare.txt | grep carping
curl https://archive.apache.org/dist/spark/spark-$SPARK_VERSION/$PACKAGE.tgz | tar -xvz -C /opt

Rest Client: Useful Command Flags

curl -X DELETE localhost:9200/myindex
-H 'Content-Type: application/json'
-d '{"type": "s3"}'
> seq 100 | curl -d @- -XPUT 'http://nsqd:4151/pub?topic=test'
-u elastic:$es_password
curl
--connect-timeout 5 # Max time to take making a connection
--max-time 10 # Max time permitted for the whole operation
--retry 5 # Number of times to retry
--retry-delay 0 # Wait this amount of time between retries
--retry-max-time 40 # Total time making a connection before failure
http://myservice.internal/api

Parsing JSON Outputs

$ curl -Ss -k https://10.225.112.74/api/api_version
{“versions”:[“1.0”,”1.1",”1.2",”1.3",”1.4",”1.5",”1.6",”1.7",”1.8",”1.8.1",”1.9",”1.10"]}
$ curl -Ss -k https://10.225.112.74/api/api_version | jq .
{
“versions”: [
“1.0”,
“1.1”,
“1.2”,
“1.3”,
“1.4”,
“1.5”,
“1.6”,
“1.7”,
“1.8”,
“1.8.1”,
“1.9”,
“1.10”
]
}

Elasticsearch Example

curl -u “elastic:$PASSWORD” -k -X PUT “$ESHOST/_snapshot/my_s3_repository?pretty” -H 'Content-Type: application/json' -d'
{
“type”: “s3”,
“settings”: {
“bucket”: “elastic-snapshots”,
“endpoint”: “10.62.64.200”,
“protocol”: “http”,
“max_restore_bytes_per_sec”: “1gb”,
“max_snapshot_bytes_per_sec”: “200mb”
}
}
'
curl -X DELETE -u "elastic:$PASSWORD" -k "$ESHOST/oops_index?pretty"

Summary

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store