cURL CLI Sheet: HTTP Headers & REST API

πŸ’‘ cURL CLI Sheet: HTTP Headers & REST API β€” Executive Summary

Technical architectural analysis, practical configuration manifests, and deployment instructions for cURL CLI Sheet: HTTP Headers & REST API.

Exec
bash
#curl test 
curl -sSL -o /dev/null -D - ${URL}

#download λ°›κΈ°(wget)
curl -LO ${URL}
curl -L ${URL} -o ${FILENAME}

#response Header 포함 좜λ ₯
curl -i

#user 인증정보 포함
curl -u id:pw https://{ URL }

#X509 인증 λ¬΄μ‹œ
curl -k https:{ URL }

#verbose
curl -v { url }

#cacert cert key
curl --cacert *.pem --key *.pem --cert *.pem {{ url }}

#jq(json to pretty)
curl {url} | jq ''

#request Header
curl -H"Name: Value"
curl --header "Name: Value"

#request body
curl -d"{}"
curl --data "{}"

header
bash
-H "Authorization: bearer ${token}"
-H "Authorization: basic $(echo "${username}:${password}" | base64)"
-H "Content-Type: application/json" #νŽ˜μ΄λ‘œλ“œμ˜ 데이터 νƒ€μž…
-H "Accept: application/json" #λ‚΄κ°€ 리슀폰슀 받을 수 μžˆλŠ” νƒ€μž…
-H "Host: ${domain}" #ip둜의 μš”μ²­μ— domain을 μ”ŒμšΈ 수 있음.
flags
bash
-L, --location
               (HTTP) μ„œλ²„κ°€ μš”μ²­ν•œ νŽ˜μ΄μ§€κ°€ λ‹€λ₯Έ μœ„μΉ˜λ‘œ μ΄λ™ν–ˆλ‹€κ³  λ³΄κ³ ν•˜λŠ” 경우(Lo-
               μ–‘μ΄μ˜¨: 헀더 및 3XX 응닡 μ½”λ“œ), 이 μ˜΅μ…˜μ„ μ‚¬μš©ν•˜λ©΄ 컬이 μƒˆ μœ„μΉ˜μ—μ„œ μš”μ²­μ„ λ‹€μ‹œ μ‹€ν–‰ν•˜κ²Œ λ©λ‹ˆλ‹€. λ§Œμ•½μ—
               -i, --include λ˜λŠ” -I, --head와 ν•¨κ»˜ μ‚¬μš©ν•˜λ©΄ μš”μ²­λœ λͺ¨λ“  νŽ˜μ΄μ§€μ˜ 헀더가 ν‘œμ‹œλ©λ‹ˆλ‹€. 인증할 λ•Œβ€
               컬이 μ‚¬μš©λ˜λ©΄ 컬은 자격 증λͺ…을 초기 ν˜ΈμŠ€νŠΈμ—λ§Œ λ³΄λƒ…λ‹ˆλ‹€. λ¦¬λ””λ ‰μ…˜μœΌλ‘œ 인해 컬이 λ‹€λ₯Έ 경우
               ν˜ΈμŠ€νŠΈμ—μ„œλŠ” μ‚¬μš©μž+λΉ„λ°€λ²ˆν˜Έλ₯Ό κ°€λ‘œμ±Œ 수 μ—†μŠ΅λ‹ˆλ‹€. λ³€κ²½ 방법에 λŒ€ν•΄μ„œλŠ” --location-trusted도 μ°Έμ‘°ν•˜μ„Έμš”.
               이것. --max-redirs μ˜΅μ…˜μ„ μ‚¬μš©ν•˜μ—¬ 따라야 ν•  λ¦¬λ””λ ‰μ…˜ 양을 μ œν•œν•  수 μžˆμŠ΅λ‹ˆλ‹€.

               컬이 λ¦¬λ””λ ‰μ…˜μ„ λ”°λ₯΄κ³  μš”μ²­μ΄ POST인 경우 λ‹€μŒκ³Ό 같은 μš”μ²­μ„ GETκ³Ό ν•¨κ»˜ λ³΄λƒ…λ‹ˆλ‹€.
               HTTP 응닡은 301, 302 λ˜λŠ” 303μž…λ‹ˆλ‹€. 응닡 μ½”λ“œκ°€ λ‹€λ₯Έ 3xx μ½”λ“œμΈ 경우 컬은
               μˆ˜μ •λ˜μ§€ μ•Šμ€ λ™μΌν•œ 방법을 μ‚¬μš©ν•˜μ—¬ λ‹€μŒ μš”μ²­μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

               μ „μš© μ˜΅μ…˜μ„ μ‚¬μš©ν•˜μ—¬ 30x 응닡 후에 POST μš”μ²­μ„ GET으둜 λ³€κ²½ν•˜μ§€ μ•Šλ„λ‘ 컬에 μ§€μ‹œν•  수 μžˆμŠ΅λ‹ˆλ‹€.
               즉, --post301, --post302 및 --post303μž…λ‹ˆλ‹€.

               -X, --request둜 μ„€μ •λœ λ©”μ†Œλ“œλŠ” 컬이 μ‚¬μš©ν•˜κΈ° μœ„ν•΄ μ„ νƒν•˜λŠ” λ©”μ†Œλ“œλ₯Ό λŒ€μ²΄ν•©λ‹ˆλ‹€.

               예:
                컬 -L https://example.com

               --resolve 및 --alt-svc도 μ°Έμ‘°ν•˜μ„Έμš”.
Features
Remove stdout
bash
# λ‘˜ λ‹€ 동일함
curl $ep -o /dev/null
curl $ep 1>/dev/null
Private domain, private rootca certificate /etc/hosts without registration https wget
bash
curl -LO https://${domain}/${path}/${upstream_file} \
     --cacert ${cacert} \
     --resolve ${domain}:443:${ip}
Send json file via POST
bash
vi ./test.json

curl -X POST https://wh.jandi.com/connect-api/webhook/20317820/626c58e4bc0497e2aa35492d01518385 \
-H "Accept: application/vnd.tosslab.jandi-v2+json" \ #λ¦¬ν€˜μŠ€νŠΈλ°”λ”” λ°±μ—”λ“œκ°€ 이해가λŠ₯ν•œ νƒ€μž… λͺ…μ‹œ
-H "Content-Type: application/json" \ #리슀폰슀 받을 μš”μ²­νƒ€μž… μ§€μ •
--data @test.json # file
Send to function (in the shell)
bash
#!/bin/bash

#ceph-df-print file export path
CEPH_DF_PRINT=/Enterprise Cloud_package/jandi/ceph-df-print

#function: read ceph-df-print file and display
generate_post_data() {
  cat << EOF
{
  "body": "[Ceph] μƒνƒœ 쑰회 ($(date "+%Y-%m-%d %I:%M:%S %p"))",
  "connectColor": "#FAC11B",
  "connectInfo": [
    {
      "title": "Ceph HOST Status",
      "description": "$(cat $CEPH_DF_PRINT | grep -e host -e root | awk 'NR==1 { print "NAME", "TOTAL", "AVAIL", "USAGE"} {gsub(/"/,""); print "["$19"-"$20"]",$4$5, $14$15,$8$9 "(" $16  "%)" }' | column -t -s' ' | awk '{printf "%s\\n", $0}')"
    },
    {
      "title": "Ceph OSD Status",
      "description": "$(cat $CEPH_DF_PRINT | grep osd | sort -r -k 17 | awk 'NR==1 { print "NAME", "TOTAL", "AVAIL", "USAGE"} {gsub(/"/,""); print "["$21"]", $5$6, $15$16, $9$10 "(" $17  "%)" }' | column -t -s' ' | awk '{printf "%s\\n", $0}')"
    }
  ]
}
EOF
}
curl -X POST https://wh.jandi.com/connect-api/webhook/20317820/7e31d57c8d58bc28151e73926a371ad9 \
-H "Accept: application/vnd.tosslab.jandi-v2+json" \
-H "Content-Type: application/json" \
--data "$(generate_post_data)"

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *