Curl Basics


Curl Basics for testing Http

Execute GET

curl -X GET "https://www.example.org/"

Execute GET with encoded query parameters

curl -X GET -G "https://www.example.org/" --data-urlencode "query1=foo+bar" --data-urlencode "query2=my&special" --data-urlencode "query2=second&value"

Execute POST

curl -X POST "https://www.example.org/"

Ignore SSL Error

curl --insecure -X GET "https://www.example.org/"

Use Http Proxy

curl -X POST "https://www.example.org/" --proxy "http[s]://[user:password@]proxyhost[:port]"

Latency / Response time

curl -X GET "https://www.example.org/" -s -o /dev/null -w 'Establish Connection: %{time_connect}s\nReceive First Byte: %{time_starttransfer}s\nTotal: %{time_total}s\n'

Timeouts

curl --max-time 5.5 --connect-timeout 2.5 "https://example.org/"

--max-time is the total time to be spent in seconds
--connect-timeout is the maximum time in seconds to establish a connection with remote server

Retry with curl until successful response is received

curl --retry 300 --retry-delay 1 --retry-all-errors "https://example.org/"

--retry max retry times
--retry-delay delay in seconds between each retry
--retry-all-errors includes domain lookup errors, etc

Show Request and Response headers

curl -v -X GET "https://www.example.org/"

Show only Response headers

curl -i -X GET "https://www.example.org/"

Http Form Post with Parameters (application/x-www-form-urlencoded)

curl --header "Content-Type: application/x-www-form-urlencoded" -X POST "https://www.example.org/" --data-urlencode 'param1=value1' --data-urlencode 'param2=value2'

Http Form Post with Parameters (multipart/form-data)

curl --header "Content-Type: multipart/form-data" -X POST "https://www.example.org/" -F "image=@nikes.png;type=image/png" -F "brand=nike" -F "color=red" -F "size=11"

Http Post with inline Json Body

curl --header "Content-Type: application/json; charset=utf-8" -X POST "https://www.example.org/" --data-binary '{"param1":"valu1","param2":"value2"}'

Http Post with inline Json Body (with variable substitution)

PARAM1_VALUE="Some Value"

curl --header "Content-Type: application/json; charset=utf-8" -X POST "https://www.example.org/" --data-binary "{\"param1\":\"$PARAM1_VALUE\",\"param2\":\"value2\"}"

Http Post with Json Body from file

curl -s --header "Content-Type: application/json; charset=utf-8" -X POST "https://www.example.org/" --data-binary @/full/or/relative/path/request.json --output /full/or/relative/path/response.json

Multiline curl

curl -X POST "https://www.example.org/" \
    --header "Content-Type: application/json; charset=utf-8" \
    --data-binary \
    '{ \
        "param1":"value1", \
        "param2":"utf 8 copyrights special character \u00A9" \
    }'

Http with basic authentication

Password embedded in the command, useful for automation

curl --basic -u 'username:password' -X GET "https://www.example.org/"

Password prompted after executing the command, useful for manual execution

curl --basic -u 'username' -X GET "https://www.example.org/"

Follow redirect

curl -L -X GET "https://www.example.org/"

Support Cookies

curl -v -L -X GET "https://www.example.org/" --cookie cookie_store.txt --cookie-jar cookie_store.txt

Ignore lengthy output and see only request and response headers

curl -v -L -o /dev/null -X GET "https://www.example.org/"

See only the status code from the response

curl -s -L -o /dev/null -w "%{http_code}" -X GET "https://www.example.org/"

Change User Agent

curl -v -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3518.0 Safari/537.36" -X GET "https://www.example.org/"

Store the response body to a file

curl -s -o output.html -X GET "https://www.example.org/"

Connection keep alive

curl --keepalive-time 50 "http://www.example.org"

Keeps the connection alive fo 50 seconds, default is 60 seconds

curl --no-keepalive "http://www.example.org"

Will close the connection after every request and use new connection for subsequent request.

Suppress Progress Information

curl -s -X GET "https://www.example.org/"

Pretty Print JSON

curl -s -X GET "https://www.example.org/somejson" | python -m json.tool

Pretty Print XML using xmllink

curl -s -X GET "https://www.example.org/somexml" | xmllint --format -

Pretty Print XML using tidy

curl -s -X GET "https://www.example.org/somexml" | tidy -iq -xml

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux