curl ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ๋ฒ•
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋””์ง€ํ„ธ ๊ฟ€ํŒ

curl ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ๋ฒ•

by ์ผ์ƒ์„ ๊ณต์œ ํ•จ๋‹ˆ๋‹ค 2022. 5. 18.

๋ชฉ์ฐจ

     

    ๊ฐœ์š”

    curl ์€ command line ์šฉ data transfer tool ์ด๋‹ค. download/upload ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋ฉฐ HTTP/HTTPS/FTP/LDAP/SCP/TELNET/SMTP/POP3 ๋“ฑ ์ฃผ์š”ํ•œ ํ”„๋กœํ† ์ฝœ์„ ์ง€์›ํ•˜๋ฉฐ Linux/Unix ๊ณ„์—ด ๋ฐ Windows ๋“ฑ ์ฃผ์š”ํ•œ OS ์—์„œ ๊ตฌ๋™๋˜๋ฏ€๋กœ

    ์—ฌ๋Ÿฌ ํ”Œ๋žซํผ๊ณผ OS์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ libcurl ์ด๋ผ๋Š” C ๊ธฐ๋ฐ˜์˜ library ๊ฐ€ ์ œ๊ณต๋˜๋ฏ€๋กœ C/C++ ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ์‹œ ์œ„์˜ protocol ๊ณผ ์—ฐ๊ณ„๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด libcurl ์„ ์‚ฌ์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ์—ฐ๊ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

    libcurl์€ PHP, ruby, PERL ๋ฐ ์—ฌ๋Ÿฌ ์–ธ์–ด์— ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด๋‚˜ ๊ฐœ๋ฐœํ™˜๊ฒฝ์— ๋งž๊ฒŒ libcurl ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
     

    ์„ค์น˜

    Linux๋‚˜ Mac OS X ์—๋Š” ๊ธฐ๋ณธ ํƒ‘์žฌ๋˜์–ด ์žˆ๋‹ค. Windows๋Š” build ๋œ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด๋„ ๋˜๊ณ  compiler ๊ฐ€ ์žˆ๋‹ค๋ฉด ์†Œ์Šค๋ฅผ ๋ฐ›์•„์„œ ์ง์ ‘ ๋นŒ๋“œํ•ด๋„ ๋œ๋‹ค.(curl windows ์—์„œ ๋นŒ๋“œํ•˜๊ธฐ ์ฐธ์กฐ)

    Windows ๋ฒ„์ „์€ cygwin๋‚˜ MinGW ๋กœ ๋นŒ๋“œํ•œ๊ฑฐ ๋ณด๋‹ค๋Š” VIsual Studio ๋กœ ๋นŒ๋“œํ•œ ๋ฒ„์ „์„ ๋‹ค์šด๋ฐ›๋Š”๊ฒŒ ์ข‹๋‹ค. (Win32-Generic, Win64 - Generic ํ•ญ๋ชฉ์—์„œ ๋ฐ›์œผ๋ฉด ๋˜๋ฉฐ ์ถ”์ฒœํ•˜๋Š” ๋งํฌ๋Š” https://winampplugins.co.uk/curl/ ์ด๋‹ค.) 

    CentOS/RHEL ๊ณ„์—ด์—์„œ ์ตœ์‹  ๋ฒ„์ „ ์„ค์น˜๋Š” RHEL/CentOS 6 ์—์„œ curl ์ตœ์‹  ๋ฒ„์ „ ์„ค์น˜ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

     

    ์ฃผ์š” ์˜ต์…˜

    ์˜ต์…˜์„ค๋ช…

    -k https ์‚ฌ์ดํŠธ๋ฅผ SSL certificate ๊ฒ€์ฆ์—†์ด ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
    -L ์„œ๋ฒ„์—์„œ HTTP 301 ์ด๋‚˜ HTTP 302 ์‘๋‹ต์ด ์™”์„ ๊ฒฝ์šฐ redirection URL ๋กœ ๋”ฐ๋ผ๊ฐ‘๋‹ˆ๋‹ค.
    --max-redirs ๋’ค์— ์ˆซ์ž๋กœ redirection ์„ ๋ช‡ ๋ฒˆ ๋”ฐ๋ผ๊ฐˆ์ง€ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ๊ธฐ๋ณธ ๊ฐ’์€ 50์ž…๋‹ˆ๋‹ค
    -v ๋™์ž‘ํ•˜๋ฉด์„œ ์ž์„ธํ•œ ์˜ต์…˜์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
    -o curl ์€ remote ์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํƒ€๋ฅผ -o ์˜ต์…˜ ๋’ค์— ์ ์–ด์ค€ FILE ๋ช…์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    -d HTTP Post ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํƒ€๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
    -u HTTP Basic Auth์— ์‚ฌ์šฉํ•  Id ์™€ ์•”ํ˜ธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
    -H ์ „์†ก์‹œ ์ถ”๊ฐ€ํ•  HTTP Header ๋ฅผ ๊ธฐ์ˆ ํ•ฉ๋‹ˆ๋‹ค.

     

     

    ๋ฐ˜์‘ํ˜•

     

    ์„œ๋ฒ„์—์„œ ๋‹ค์šด๋กœ๋“œ

    ๋Œ€๋ฌธ์ž -O ์˜ต์…˜์„ ์ฃผ๋ฉด URL์˜ ๋งจ ๋’ค์— ์žˆ๋Š” resource ๋ช…์ธ bc-1.07.1.tar.gz ๋กœ ํŒŒ์ผ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

    curl -O http://ftp.gnu.org/gnu/bc/bc-1.07.1.tar.gz

     

    ์†Œ๋ฌธ์ž -o ์˜ต์…˜ ๋’ค์— ์ง€์ •ํ•œ filename ์œผ๋กœ ์ €์žฅํ•˜๋ฉฐ ์•„๋ž˜ ์˜ˆ์ œ๋Š” bc.tgz ๋กœ ํŒŒ์ผ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

    curl -o bc.tgz http://ftp.gnu.org/gnu/bc/bc-1.07.1.tar.gz

    HTTP Basic auth

     

    -u ๋’ค์— ๋กœ๊ทธ์ธํ•  id(userid)์™€ ์•”ํ˜ธ(password)๋ฅผ ์ง€์ •ํ•˜๋ฉฐ ๊ตฌ๋ถ„์„ ์œ„ํ•ด : ๋ฅผ ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    curl -u userid:password http://www.example.com/user.html

     

    HTTP Bearer token ์ธ์ฆ

    OAuth ๋‚˜ JWT ๋“ฑ์— ์‚ฌ์šฉํ•˜๋Š” Bearer token ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด -H ์˜ต์…˜๋’ค์—  'Authorization: Bearer {TOKEN}' ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉฐ {TOKEN} ์€ ์‹ค์ œ ํ† ํฐ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋˜๋ฉฐ ์•„๋ž˜๋Š” 12345 ๋ผ๋Š” token ์— ์„œ๋ฒ„์— ์ „์†กํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

    curl -L -X POST -H 'Accept: application/json' -H 'Authorization: Bearer 12345' 'https://www..example.com/api/myresource'

     

     

    POST ๋กœ data ์ „์†ก

     

    ๋ฐ์ดํƒ€ ํŒŒ์ผ์„ ๋ณด๋‚ด๋Š” -d ์˜ต์…˜ ๋’ค์— ์ „์†กํ•  json ๋ฐ์ดํƒ€๋ฅผ ์ ์–ด์ฃผ๊ณ  ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” -H ์˜ต์…˜์œผ๋กœ "Accept: application/json" ์™€ "Content-Type: application/json" ๋ฅผ ๊ธฐ์ˆ ํ•ฉ๋‹ˆ๋‹ค.

    curl -u userid:password -L -v -d '{"name": "superman", "age" : 30}' -H "Accept: application/json" -H "Content-Type: application/json" 'https://postman-echo.com/post'

     

     

    ํฐ ๋ฐ์ดํƒ€์˜ ๊ฒฝ์šฐ ์ปค๋งจ๋“œ์— ๋‚ด์šฉ์„ ์ ์–ด์ค„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ณ  -d ์˜ต์…˜๋’ค์— ํŒŒ์ผ์„ ์ง€์ •ํ•˜๋Š” ํ‚ค์›Œ๋“œ์ธ @FILENAME ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” my-json-file ๋‚ด์šฉ์„ json ํ˜•์‹์œผ๋กœ ๋ณด๋‚ด๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

    curl -u userid:password -L -v -d @my-json-file -H "Accept: application/json" -H "Content-Type: application/json" 'https://postman-echo.com/post'

     

     

    jq ์™€ ์—ฐ๋™

    ๋ช…๋ นํ–‰ JSON ์ฒ˜๋ฆฌ๊ธฐ์ธ jq ๋ฅผ ํ™œ์šฉํ•ด์„œ ์„œ๋ฒ„์˜ JSON ์‘๋‹ต์„ ํฌ๋งทํŒ…

    curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq .

     

     

    ์„œ๋ฒ„์˜ json ๋ฐฐ์—ด์ค‘ 3๋ฒˆ์งธ ๋ฐฐ์—ด ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

    curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[2]'

     

     

    salesforce REST API

    API ๋ชฉ๋ก ์–ป๊ธฐ

    curl http://na1.salesforce.com/services/data/
    [
    { "label": "Winter '11", "url": "/services/data/v20.0", "version": "20.0" },
    { "label": "Spring '11", "url": "/services/data/v21.0", "version": "21.0" },
    { "label": "Summer '11", "url": "/services/data/v22.0", "version": "22.0" },
    { "label": "Winter '12", "url": "/services/data/v23.0", "version": "23.0" },
    { "label": "Spring '12", "url": "/services/data/v24.0", "version": "24.0" },
    { "label": "Summer '12", "url": "/services/data/v25.0", "version": "25.0" },
    { "label": "Winter '13", "url": "/services/data/v26.0", "version": "26.0" },
    { "label": "Spring '13", "url": "/services/data/v27.0", "version": "27.0" },
    { "label": "Summer '13", "url": "/services/data/v28.0", "version": "28.0" },
    { "label": "Winter '14", "url": "/services/data/v29.0", "version": "29.0" },
    { "label": "Spring '14", "url": "/services/data/v30.0", "version": "30.0" }
    ]


     

    ์ถœ์ฒ˜ : Software Architect (https://www.lesstif.com/)

     

     

    Node JS ,express ํŒจํ‚ค์ง€๋กœ ์›น์„œ๋ฒ„ ๊ตฌ๋™ํ•˜๊ธฐ

    NodeJs ๊ธฐ์ดˆ ์•Œ์•„๊ฐ€๊ธฐ ์ž๋™ํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” Selenium ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ node ,python์„ ์‚ฌ์šฉํ•˜์—ฌ ์›น์„ ์ปจํŠธ๋กค ํ•ฉ๋‹ˆ๋‹ค. ํฌ๋กฌ๋„ ๊ตฌ๊ธ€์—์„œ ๋งŒ๋“  ์–ธ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— GAS (Google App Script) ๋กœ Chrome์„ ์ œ์–ดํ•  ์ˆ˜ ์—†๋‚˜ ์ƒ๊ฐ

    odaily.tistory.com

     

    [์นด์นด์˜ค๋ทฐ] ๋ณด๋“œ ์ž๋™ ๋ฐœํ–‰ 3๋‹จ๊ณ„. nodeJS๋กœ selenium ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜์—ฌ ์นด์นด์˜ค๋ทฐ - ์ฑ„๋„ ์ž๋™ ๋กœ๊ทธ

    python๋„ ์ข‹์ง€๋งŒ nodejs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณด๋“œ๋ฐœํ–‰์„ ์ž๋™ํ™” ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋Š˜ ๋‚ด์šฉ์—๋Š” ๋กœ๊ทธ์ธ๊นŒ์ง€๋งŒ ๊ธฐ์žฌํ•˜๊ณ , ์˜์ƒ์€ ๋ง›๋ณด๊ธฐ๋กœ ์ƒ˜ํ”Œ ์˜ˆ์ œ๋กœ ๊ณต๊ฐœ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ์ดˆ๋ณด์ž๋Š” ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์ง€๋งŒ ์ตœ๋Œ€ํ•œ ๋งŽ์€

    odaily.tistory.com

     

    ๋Œ“๊ธ€