cURL

From XennisWiki
Jump to: navigation, search

cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name stands for "Client URL". (Wikipedia)

Installation

Debian

apt-get install -y curl

Usage

Parameter Description
--connect-timeout <seconds> Timeout in seconds
--location Follow redirect
--include [-i] Output HTTP header
--insecure Allow insecure certificates
--silent Quit outout
--user <user>:<pass> Basic auth
--header '<key>: <value>' Header

Download a file

Further information: wget vs curl: How to Download Files Using wget and curl

curl -O http://example.org/sample.zip

(With wget it is wget http://example.org/sample.zip)

Download a file silently with a output name

curl --output local-name.txt --silent http://example.org/sample.zip

POST request

HTTP POST sending JSON with authorization header

data='{"count": 10, "name": "text"}'
curl http://example.org/endpoint \
    --request POST \
    --header 'Content-Type: application/json' \
    --header "Authorization: Bearer ${JWT}" \
    --data $data

Tips

Parsing JSON with jq

Further information: jq Turotial

On Arch Linux install the jq. Just pipe the output of cURL into jq

curl 'https://example.org/my/api' | jq '.'

Query the result like

curl 'https://example.org/my/api' | jq '.cars[0].color'

Get the same result without the quotes

curl 'https://example.org/my/api' | jq --raw-output '.cars[0].color'

See also

External links