cURL is a computer software project providing a library (libcurl) and Command Line Interfacetool (curl) for transferring data using various network protocols - like HTTP. The name stands for “Client URL”.

Save Result to Bash Variable

TOKEN="XXX"
RESULT=$(curl -d @- -H "X-Auth: $TOKEN" \
         "https://httpbin.org/post" <<PAYLOAD
[{
    "key": "value"
}]
PAYLOAD)

echo "$RESULT"

REST JSON

Basic Auth

# Can specify in the url
curl https://name:passwd@httpbin.org/basic-auth/name/passwd
# or seperately:
curl -u name:passwd https://httpbin.org/basic-auth/name/passwd

GET JSON

curl -i \
    -H "Accept: application/json" -H "Content-Type: application/json" \
    -X GET "https://httpbin.org/get"

PUT JSON

curl -i \
     -H 'Content-Type: application/json' -H 'Accept: application/json' \
     -X PUT \
     -d '{"updated_field1":"updated_value1"}' "https://httpbin.org/put"

POST JSON

curl -i -H 'Accept: application/json' \
     -X POST -F "filename=@/file/path" \ 
     -F "other_field=its_value"   "https://httpbin.org/post"

DELETE JSON

curl -i \
     -H 'Content-Type: application/json' -H 'Accept: application/json' \
     -X DELETE -d '{"key1":"value1"}' "https://httpbin.org/delete"

Using Basic Auth

curl --user myuser:secret http://example.com/

Save file

curl --silent https://www.example.com/pkg.bin -o pkg.bin

Combining Curl with other commands

curl -X GET "https://git.ballyda.com/api/v1/user/repos" -H  "accept: application/json" | jq -r '.[] | .name +" "+ .description' | grep "not really" | sed 's/\snot\sreally\sknown\(.*\)/\L\1/' > bad-repos.txt
# Then we can process the file in another CURL call:
<bad-repos.txt xargs -I % curl -X DELETE https://git.ballyda.com/api/v1/repos/dueyfinster/% -H  "accept: application/json"

See also: