Using Curl
A network programmer's best friend
Curl is a command line tool for issuing network requests. It is extremely valuable when debugging network protocols (e.g. HTTPS) and plays in important role in DevOps when scripting API calls.
Example: Install a release package from GitHub
curl -JLO output.html <url>
# or
curl -Lo output.html <url>
curl -JLO output.html <url>
# or
curl -Lo output.html <url>
The -o
flag saves the response to the file specified. Alternatively, the -O
flag saves it using a filename extracted from the path. Finally the -J
option tells -O
to use the filename on the server (I often just keep it with).
curl -o output.html <url> # -> saves as output.html
# versus
curl -O <sub-url>/index.html # -> saves as index.html
# finally
curl -JO <sub-url>/index.html # -> may save as index.html, or something else
curl -o output.html <url> # -> saves as output.html
# versus
curl -O <sub-url>/index.html # -> saves as index.html
# finally
curl -JO <sub-url>/index.html # -> may save as index.html, or something else
Follow a Redirect
When a redirect response (e.g. status code 301) contains the Location
header field, the redirect can be followed with the -L
flag
curl -L <url>
curl -L <url>
HTTP GET Request
curl <url>
curl <url>
Get HTTP Respose Headers
curl -i <url>
# or for response headers *only*
curl -I <url>
curl -i <url>
# or for response headers *only*
curl -I <url>
HTTP Generic Request
Change the type of the HTTP request using the -X/--request
flag followed by one of: GET
, POST
, DELETE
, PUT
. To provide query parameters to the <url>
add the -d/--data
option.
curl -d "what=nobs&book=techbook" -X <url>
curl -d "what=nobs&book=techbook" -X <url>
HTTP POST Request Sending JSON
Set the Content-Type header with the --header/-H
flag
curl --data '{"what": "nobs", "book": "techbook"}' \
--header "Content-Type: application/json" \
--request POST <url>
curl --data '{"what": "nobs", "book": "techbook"}' \
--header "Content-Type: application/json" \
--request POST <url>
The contents from a JSON file can also be used
curl -d "@content.json" -X POST <url>
curl -d "@content.json" -X POST <url>
HTTP Authentication
curl -u <username>:<password> <url>
curl --header "Authorization: Bearer <token>" <url>
curl -u <username>:<password> <url>
curl --header "Authorization: Bearer <token>" <url>
Example curl with headers
curl -u bob:password123 -i -H "content-type:application/json" -X GET https://fancyurl.com/extra-bit
curl -u bob:password123 -i -H "content-type:application/json" -X GET https://fancyurl.com/extra-bit
Notes
- Use the
--verbose
option to provide all information about the request and response
ADDITIONAL RESOURCES