Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/23daa240b95f428856a048b4bbaf3e3b to your computer and use it in GitHub Desktop.
Save dacr/23daa240b95f428856a048b4bbaf3e3b to your computer and use it in GitHub Desktop.
curl cheat sheet / published by https://github.com/dacr/code-examples-manager #8170fd16-e03f-4e76-99b6-1cf8472a0f18/613a3a746cd61f95525572cb23cb73a61a5fb183

curl cheat sheet

Get the redirection full URL

curl -Ls -o /dev/null -w %{url_effective} https://mapland.fr/echo

Post JSON

curl -d '{"message":"hello"}' -H "Content-Type: application/json"  https://httpbin.org/post

echo '{"message":"hello"}' > file.json
curl -d @file.json -H "Content-Type: application/json"  https://httpbin.org/post

Parameterized json

How to generate customized / parameterized json from shell :

MSG="Hello world"

generate_json() {
  cat <<EOF
{
  "message": {
    "content":"$MSG"
  } x 
}
EOF
}

curl -s -XPOST "https://httpbin.org/post" -H 'Content-Type: application/json' --data "$(generate_json)" | jq ".json"

basic authentication with curl

curl -u "${BASIC_AUTH_USER}:${BASIC_AUTH_PASSWORD}" $URL

basic authentication using token with curl

AUTH=$(echo -ne "${BASIC_AUTH_USER}:${BASIC_AUTH_PASSWORD}" | base64 --wrap 0)
curl \
  --header "Content-Type: application/json" \
  --header "Authorization: Basic $AUTH" \
  --request POST \
  --data  '{"key1":"value1", "key2":"value2"}' \
  $URL
curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
  https://gitlab.com/api/v4/groups/givengroup/projects | \
  -o repositories.json


curl --header "Authorization: Bearer xxxxxxxxx" https://somewhere.comm/

to ignore SSL certificate issues

curl --insecure https://mapland.fr/

Get SSL certificate information from web remote site

curl --insecure -vvI https://www.mapland.fr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment