curl ์ค์น ๋ฐ ์ฌ์ฉ๋ฒ - HTTP GET/POST, REST API ์ฐ๊ณ๋ฑ
์ด ํ์ด์ง ๋ด์ฉ์ด ๋๋ฌด ๋ง์์ ธ์ curl ์ฃผ์ ์ฌ์ฉ๋ฒ ์์ฝ ์ ์ฃผ์ ๋ด์ฉ๋ง ์ถ์ฝํ์ต๋๋ค.
Web ์์ ํ์ผ์ ๋ค์ด๋ฐ์ ๊ฒฝ์ฐ wget ์ด ๋ ์ฌ์ฉ์ด ๊ฐํธํฉ๋๋ค.
๊ฐ์
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 ์ต์ ๋ฒ์ ์ค์น ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์.
์ฃผ์ ์ต์
curl [options...] <url> ํ์์ผ๋ก ์ฌ์ฉํ๋ฉด ๋๋ค.
option ์ฒ๋ฆฌ๋ GNU getopt ๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ํ์ดํ ํ๋๋ฅผ ๋ถ์ด๋ short ํ์์ ์ต์ ๊ณผ ํ์ดํ ๋๊ฐ๋ก ์์๋๋ long ํ์์ options ์ด ์๋ค.
์ฃผ์ options (http/https ๊ด๋ จ ์ต์ ๋ง ์ ๋ฆฌ)
short | long | ์ค๋ช | ๋น๊ณ |
---|---|---|---|
-k | --insecure | https ์ฌ์ดํธ๋ฅผ SSL certificate ๊ฒ์ฆ์์ด ์ฐ๊ฒฐํ๋ค. | wget ์ --no-check-certificate ๊ณผ ๋น์ทํ ์ญํ ์ํ |
-l | --head | HTTP header ๋ง ๋ณด์ฌ์ฃผ๊ณ content ๋ ํ์ํ์ง ์๋๋ค | |
-D | --dump-header <file> | <file> ์ HTTP header ๋ฅผ ๊ธฐ๋กํ๋ค. | |
-L | --location | ์๋ฒ์์ HTTP 301 ์ด๋ HTTP 302 ์๋ต์ด ์์ ๊ฒฝ์ฐ redirection URL ๋ก ๋ฐ๋ผ๊ฐ๋ค. --max-redirs ๋ค์ ์ซ์๋ก redirection ์ ๋ช ๋ฒ ๋ฐ๋ผ๊ฐ์ง ์ง์ ํ ์ ์๋ค. ๊ธฐ๋ณธ ๊ฐ์ 50์ด๋ค | curl -v daum.net ์ ์คํํ๋ฉด ๊ฒฐ๊ณผ๊ฐ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ด HTTP 302 ๊ฐ ๋ฆฌํด๋๋ค. < HTTP/1.1 302 Object Moved -L ์ต์ ์ ์ถ๊ฐํ๋ฉด www.daum.net ์ผ๋ก ์ฌ์ ์ํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์์ค๊ฒ ๋๋ค. |
-d | --data | HTTP Post data | FORM ์ POST ํ๋ HTTP๋ JSON ์ผ๋ก ๋ฐ์ดํ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ REST ๊ธฐ๋ฐ์ ์น์๋น์ค ๋๋ฒ๊น ์ ์ ์ฉํ ์ต์ ์ด๋ค |
-v | --verbose | ๋์ํ๋ฉด์ ์์ธํ ์ต์ ์ ์ถ๋ ฅํ๋ค. | |
-J | --remote-header-name | ์ด๋ค ์น์๋น์ค๋ ํ์ผ ๋ค์ด๋ก๋์ Content-Disposition Header ๋ฅผ ํ์ฑํด์ผ ์ ํํ ํ์ผ์ด๋ฆ์ ์ ์ ์์ ๊ฒฝ์ฐ๊ฐ ์๋ค. -J ์ต์ ์ ์ฃผ๋ฉด ํค๋์ ์๋ ํ์ผ ์ด๋ฆ์ผ๋ก ์ ์ฅํ๋ค. | curl 7.20 ์ด์๋ถํฐ ์ถ๊ฐ๋ ์ต์ |
-o | --output FILE | curl ์ remote ์์ ๋ฐ์์จ ๋ฐ์ดํ๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก๋ ์ฝ์์ ์ถ๋ ฅํ๋ค. -o ์ต์ ๋ค์ FILE ์ ์ ์ด์ฃผ๋ฉด ํด๋น FILE ๋ก ์ ์ฅํ๋ค. (download ์ ์ ์ฉ) | |
-O | --remote-name | file ์ ์ฅ์ remote ์ file ์ด๋ฆ์ผ๋ก ์ ์ฅํ๋ค. -o ์ต์ ๋ณด๋ค ํธ๋ฆฌํ๋ค. | |
-s | --silent | ์ ์ ๋ชจ๋. ์งํ ๋ด์ญ์ด๋ ๋ฉ์์ง๋ฑ์ ์ถ๋ ฅํ์ง ์๋๋ค. -o ์ต์ ์ผ๋ก remote data ๋ /dev/null ๋ก ๋ณด๋ด๋ฉด ๊ฒฐ๊ณผ๋ฌผ๋ ์ถ๋ ฅ๋์ง ์๋๋ค | HTTP response code ๋ง ๊ฐ์ ธ์ค๊ฑฐ๋ ํ ๊ฒฝ์ฐ ์ ๋ฆฌ |
-X | --request | Request ์ ์ฌ์ฉํ method ์ข ๋ฅ(GET, POST, PUT, PATCH, DELETE) ๋ฅผ ๊ธฐ์ ํ๋ค. | |
-i | --include | ์๋ต์ Content ๋ง ์ถ๋ ฅํ์ง ์๊ณ ์๋ฒ์ Reponse ๋ ํฌํจํด์ ์ถ๋ ฅํ๋ค. (๋๋ฒ๊น ์ ์ ์ฉ) |
HTTP/HTTPS Download(GET Method)
HTTP๋ก ๋ค์ด๋ก๋
curl http://www.gnu.org/software/bash/manual/html_node/index.html
curl -o index.html http://www.gnu.org/software/bash/manual/html_node/index.html
curl -O http://www.gnu.org/software/bash/manual/html_node/index.html
curl -O http://www.gnu.org/software/bash/manual/html_node/index.html -O http://www.gnu.org/savannah-checkouts/gnu/libiconv/documentation/libiconv-1.13/iconv.1.html
์ด์ด๋ฐ๊ธฐ
-C/--continue-at <offset> ์ต์ ์ ์ฃผ๋ฉด ์ด์ด๋ฐ๊ธฐ ๊ฐ๋ฅ(offset ์ - ๋ฅผ ์ฃผ๋ฉด ์ ์ก์ดํ ๋ถ๋ถ๋ถํฐ ์ด์ด๋ฐ์)
curl -C - -O http://www.gnu.org/software/bash/manual/html_node/index.html
ํน์ ์ผ ์ด์ /์ดํ ๋ณ๊ฒฝ๋์์ผ๋ฉด ๋ฐ๊ธฐ
-z/--time-cond <time>
HTTP ํค๋์ If-Modified-Since: ํค๋๋ฅผ ์ถ๊ฐํ์ฌ ํน์ ์ผ ์ดํ์ ๋ณ๊ฒฝ๋์์ผ๋ฉด ๋ค์ด๋ก๋ ์ํ
์๋ ์์ ๋ 2011๋ 12์ 21์ผ ์ดํ์ ๋ณ๊ฒฝ๋์์ผ๋ฉด ๋ค์ด๋ก๋ ์ํ
curl -z 21-Dec-11 http://www.example.com/yy.html
๋ ์์์ - ๋ฅผ ์ถ๊ฐํ๋ฉด If-Unmodified-Since: ํค๋๋ฅผ ์ถ๊ฐํ์ฌ ํน์ ์ผ ์ด์ ์ ๋ณ๊ฒฝ๋์์ผ๋ฉด ๋ค์ด๋ก๋ ์ํ
์๋ ์์ ๋ 2011๋ 12์ 21์ผ ์ด์ ์ ๋ณ๊ฒฝ๋์์ผ๋ฉด ๋ค์ด๋ก๋ํจ (๋ ์์ - ์ถ๊ฐ)
curl -z -21-Dec-11 http://www.example.com/yy.html
http ์๋ต ์ฝ๋๋ง ์ถ๋ ฅ
HTTP Header ๋ contents ๋ ๋นผ๊ณ HTTP Response code ๋ง ์ถ๋ ฅํ๋ค. ์๋ฒ์ ์ ์ ์๋ ์ฌ๋ถ ์ ๊ฒ๋ ์ ์ฉํ๋ค.
curl -L -k -s -o /dev/null -w "%{http_code}\n" http://www.example.com/yy.html
์๋ฒ์ ์ ์ ๋์ ์ฌ๋ถ๋ฅผ ์ ๊ฒํ ๊ฒฝ์ฐ SSL ์ธ์ฆ์ ๊ฒ์ฆ ์ ์ธ(-k) ์ 301, 302 Redirect ์ ๋ฐ๋ผ ๊ฐ๋ ์ต์ (-L)์ ์ถ๊ฐํ๋ ๊ฒ์ด ์์ ํ๋ค.
์ด๋ฅผ ํ์ฉํ์ฌ ์๋์ฒ๋ผ ๊ฐ๋จํ ์คํฌ๋ฆฝํธ๋ก ์น ์๋ฒ์ ์ ์ ๋์ ์ฌ๋ถ๋ฅผ ํ์ธํ ์ ์๋ค.
๊ฒฐ๊ณผ๊ฐ์ HTTP Header ํฌํจ(-i)
-i ,–include ์ต์ ์ ์ฌ์ฉํ๋ฉด ์๋ฒ์ ์๋ต์ ์๋ฒ๊ฐ ๋ณด๋ธ HTTP ํค๋๋ฅผ ์ถ๊ฐํ์ฌ ์ถ๋ ฅํ๋ค. ๋๋ฒ๊น ์ ์ ์ฉํ๋ค.
curl -i https://api.github.com -u valid_username:valid_password
jq ์ ์ฐ๋ํด์ ์๋ฒ์ JSON ํ์ฑ
๋ช ๋ นํ json ์ฒ๋ฆฌ๊ธฐ์ธ jq ๋ฅผ ์ฐ๋ํด์ ์๋ฒ์ JSON ์๋ต์ ๋ณด๊ธฐ ์ข๊ฒ ํ์ฑ
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq .
๋ฐฐ์ด๋ก ๋์ด์จ ์๋ต์ค ์ฒซ ๋ฒ์งธ ์์ ์ถ์ถ
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[1]'
HTTP ์ธ์ฆ
Basic Auth
id/pwd ๊ฐ ํ์ํ ์ฌ์ดํธ์ ๊ฒฝ์ฐ -u(–user) ์ต์ ๋ค์ userid:password ๋ฅผ ์ง์ ํ์ฌ ์ธ์ฆํ ์ ์๋ค.
curl -v -u userid:password http://www.example.com/user.html
Bearer token ์ธ์ฆ
OAuth ๋ JWT ๋ฑ์ ์ฌ์ฉํ๋ Bearer token ์ ์ฌ์ฉํ๋ ค๋ฉด -H ์ต์ ๋ค์ 'Authorization: Bearer {TOKEN}' ๋ฅผ ์ถ๊ฐํ๋ฉฐ {TOKEN} ์ ์ค์ ํ ํฐ์ผ๋ก ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
์๋๋ 12345 ๋ผ๋ token ์ ์๋ฒ์ ์ ์กํ๋ ์์ ์ด๋ค.
curl -v -L -X POST -H 'Accept: application/json' -H 'Authorization: Bearer 12345' 'https://www..example.com/api/myresource'
POST/File Upload(POST, PUT)
File Upload(PUT)
-T, --upload-file ์ต์ ์ ์ฌ์ฉํ๋ฉด ์๋ฒ์ ํ์ผ์ ์ ์กํ ์ ์์. ์๋๋ a.pdf ๋ฅผ ์๋ฒ์ ์ ์กํ๋ ์์
curl -T a.pdf http://posttestserver.com/post.php
-T ๋ก ํ์ผ์ ์ ์กํ๋ฉด POST ๊ฐ ์๋ PUT Request ๋ก ์ฒ๋ฆฌํจ.
์ง์ PUT ์ผ๋ก method ๋ฅผ ์คํํ ๊ฒฝ์ฐ -X PUT ์ต์ ์ ์ถ๊ฐํด์ URL ์ ํธ์ถ.
curl -L -X PUT 'https://postman-echo.com/put' \ --data-raw 'This is expected to be sent back as part of response body.'
HTTP FORM POST
-X POST ์ต์ ์ ์ถ๊ฐํ๊ฑฐ๋ -d( --data) ์ต์ ์ ์ง์ ํ๋ฉด ๊ธฐ๋ณธ๊ฐ์ผ๋ก POST ๋ก ์ค์ ๋จ
<form method="POST" action="post.php"> <input type=text name="first_name"> <input type=text name="last_name"> <input type=submit name=press value=" OK "> </form>
POST ๋ฐ์ดํ๋ "param1=value1¶m2=value2" ํ์์ผ๋ก ์ ๋ฌ
curl -d "first_name=Bruce&last_name=Wayne&press=%20OK%20" http://posttestserver.com/post.php
๋ฐ์ดํ์ ๊ณต๋ฐฑ์ด๋ ๊ธฐํ ํน์ ๋ฌธ์๊ฐ ์์ ๊ฒฝ์ฐ URL encoding ์ ํด์ผ ํ๋ค.
๊ณต๋ฐฑ์ผ ๊ฒฝ์ฐ ์ผ์ผ์ด + ๋ก ๋ณํํด์ ์ ์กํด์ผ ํ์ง๋ง ์ต์ ๋ฒ์ ์ curl(7.18.0 ์ดํ) ์ FORM ํ๋ผ๋ฏธํฐ๋ฅผ URL Encoding ํด์ฃผ๋ --data-urlencode ์ต์ ์ ์ฌ์ฉํ๋ฉด ๋ณ๋๋ก ์ธ์ฝ๋ฉ์ ํด์ฃผ์ง ์์๋ ๋๋ค.
curl --data-urlencode "first_name=Bruce" --data-urlencode "last_name=Wayne" --data-urlencode "press= OK " http://posttestserver.com/post.php
Hidden field ์ ์ก์ ์ผ๋ฐ ํ๋์ฒ๋ผ name=value ํ์์ผ๋ก ์ ์กํ๋ฉด ๋๋ค.
HTTP POST data
-d, --data ์ต์ ๋ค์ ์ ์กํ ๋ฐ์ดํ๋ฅผ ๊ธฐ์
curl -L -v -d '{"name": "superman", "age" : 30}' -H "Accept: application/json" -H "Content-Type: application/json" http://posttestserver.com/post.php
-d ์ต์ ์ ์ถ๊ฐํ๋ฉด -X POST ๋ ์ ์ธ ๊ฐ๋ฅ
HTTP POST File
file์ POSTํ ๊ฒฝ์ฐ file name ์์ @ ๋ฅผ ๋ถ์ฌ์ค
curl -d @myPostfile http://posttestserver.com/post.php
HTTP POST Binary File
curl ์ POST ์ ๋ฐ์ดํ๋ฅผ text ๋ก ์ทจ๊ธํ๋ฏ๋ก binary ๋ฐ์ดํ๋ ๊นจ์ง ์ ์๋ค. ์ ๋๋ก ์ ์กํ๋ ค๋ฉด --data-binary ์ต์ ์ ์ถ๊ฐํด์ผ ํ๋ค.
curl --data-binary @myBinary.jpg http://posttestserver.com/post.php
HTTP File Upload Form
๋ค์๊ณผ ๊ฐ์ ํ์ผ ์ ๋ก๋ FORM ์ด ์์๋
<form method="POST" enctype='multipart/form-data' action="upload.php"> <input type=file name=upload> <input type=submit name=press value="OK"> </form>
localfilename ์ upload ํ ํ์ผ๋ช , submit ์ press=OK
curl --form upload=@localfilename --form press=OK http://localhost/upload.php
PATCH, DELETE ๋ฉ์๋
PATCH
Remote ์ ์๋ Resource ์ ์ ์ฒด๋ฅผ ๊ต์ฒดํ๋ PUT ๊ณผ๋ ๋ฌ๋ฆฌ ์ผ๋ถ ํญ๋ชฉ๋ง ๊ต์ฒดํ ๋ ์ฌ์ฉํ๋ ๋ฉ์๋๋ก -X PATCH ์ต์ ์ ์ถ๊ฐํด์ URL ํธ์ถ.
curl -L -X PATCH 'https://postman-echo.com/patch' \ --data-raw 'This is expected to be sent back as part of response body.'
DELETE
Remote ์ ์๋ Resource ๋ฅผ ์ญ์ ํ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ฉฐ ๋ณด์ ๋ฌธ์ ๊ฐ ์์ผ๋ฏ๋ก ์๋ฒ๋ ๊ถํ์๋ ์ฌ์ฉ์์ ํธ์ถ์ธ์ง ์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ ์ฒ๋ฆฌํด์ผ ํจ.
client ๋ -X DELETE ์ต์ ์ ์ถ๊ฐํด์ URL ํธ์ถ.
curl -L -X DELETE 'https://postman-echo.com/delete' \ --data-raw 'This is expected to be sent back as part of response body.'
HTTP Header ์ค์
ํน์ ํ HTTP Header ๋ฅผ ์ค์ ํด์ ๋ณด๋ด์ผ ํ ๊ฒฝ์ฐ(Ex: json data๋ฑ) -H (–header) ์ต์ ์ผ๋ก ํค๋๋ฅผ ์ค์ ํ ์ ์๋ค.
Content-Type Header ์ค์
curl -d @myJson.js -H "Content-Type: application/json" http://localhost:8080/jsonEcho
User-Agent ์ค์
ํน์ ๋ธ๋ผ์ฐ์ ์ธ(Browser) ๊ฒ์ฒ๋ผ ๋์ํ๊ธฐ ์ํด์๋ -A ( --user-agent) ์ต์ ์ ์ฌ์ฉํ ์ ์๋ค. (http://www.useragentstring.com/)
User-Agent Chrome 24.0
curl -d @myJson.js -H "Content-Type: application/json" --user-agent "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.14 (KHTML, like Gecko) Chrome/24.0.1292.0 Safari/537.14" http://localhost:8080/jsonEcho
User-Agent MSIE(Internet Explorer) 10.0
curl -d @myJson.js -H "Content-Type: application/json" --user-agent "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)" http://localhost:8080/jsonEcho
User-Agent Firefox 29.0
curl -d @myJson.js -H "Content-Type: application/json" --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 Firefox/29.0" http://localhost:8080/jsonEcho
Referer ์ค์
Referer ๋ฅผ ์ฒดํฌํ๋ ์ฌ์ดํธ์ผ ๊ฒฝ์ฐ -e (–referer) ์ต์ ์ผ๋ก Referer URL ์ ์ค์ ํ ์ ์๋ค.
curl --referer http://www.example.come/from http://www.example.com/to
์๋๋ฉด -H ์ต์ ์ผ๋ก referer ํค๋๋ฅผ ์ง์ ํด๋ ๋๋ค.
curl -H "Referer: http://www.example.come/from" http://www.example.com/to
Accept-Encoding ์ผ๋ก ์ปจํ ์ธ ์์ถ ์์ฒญ
์ ์ก ์๋ ํฅ์์ ์ํด mod_deflate(apache httpd) ๋ ngx_http_gzip_module ์ ์ฌ์ฉํด์ ์ปจํ ์ธ ๋ฅผ ์์ถํ ์ ์๋ค. ์น ์๋ฒ๊ฐ ์ปจํ ์ธ ์์ถ์ ์ฌ์ฉํ๋์ง ํ์ธํ๊ธฐ ์ํด curl ์คํ์ Accept-Encoding ํค๋๋ฅผ ์ค์ ํ๋ฉด ๋๋ค.
curl -I -H 'Accept-Encoding: gzip,deflate' https://www.google.com HTTP/1.1 200 OK Date: Wed, 25 Jul 2018 08:29:02 GMT Expires: -1 Cache-Control: private, max-age=0 Content-Type: text/html; charset=UTF-8 P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info." Content-Encoding: gzip Server: gws
์์ถ ์ฌ๋ถ๋ Content-Encoding: gzip ํค๋๊ฐ ์๋ฒ response ์ ํฌํจ๋์๋์ง ์ฌ๋ถ๋ก ํ์ธํ ์ ์๋ค.
SSL/TLS ์ต์
SSL/TLS ์ธ์ฆ์ ๊ฒ์ฆ ์ค์
curl์ TLS ์ฐ๊ฒฐ์ ์ธ์ฆ์์ ์ ํจ์ฑ(์ ๋ขฐ๋ ์ธ์ฆ๊ธฐ๊ด์์ ๋ฐ๊ธ ์ฌ๋ถ, ์ธ์ฆ์๋ด DN๊ณผ host์ด๋ฆ ์ผ์น ์ฌ๋ถ, ์ ํจ ๊ธฐ๊ฐ๋ด์ ์๋์ง๋ฑ)์ ๊ฒ์ฆํ์ง๋ง ๊ฐ๋ฐ์ฉ ์๋ฒ๋ผ ๋๋ฉ์ธ์ด ์์ด์ self signed ์ธ์ฆ์๊ฐ ์ค์น๋์๊ฑฐ๋
์ ํจ ๊ธฐ๊ฐ์ด ์ง๋ฌ๋๋ฐ ๊ฐฑ์ ์ ๋ชป ํ๊ฑฐ๋ ๋ฑ์ ์ด์ ๊ฐ ์์ ๊ฒฝ์ฐ ์์ฒญ์ด ์คํจํ๊ฒ ๋๋ค.
์ด๋ฐ ๊ฒฝ์ฐ -k, --insecure ์ต์ ์ ์ฌ์ฉํ๋ฉด ์ธ์ฆ์ ๊ฒ์ฆ์ ๋๊ณ TLS ์ธ์ ์ ๊ตฌ์ฑํ๊ฒ ๋๋ค.
curl -k -L http://www.example.come
์ผ๋ฐ์ ์ผ๋ก -k ๋ -L ๊ณผ ๊ฐ์ด ์ฌ์ฉํ๋ ๊ฒ์ด ํธ๋ฆฌํ๋ค.
curl ์ ์ธ์ฆ ๊ธฐ๊ด ์ธ์ฆ์๊ฐ ๋ฑ๋ก๋์ง ์์์ ๊ฒ์ฆ ์ค๋ฅ๊ฐ ๋ ๊ฒฝ์ฐ curl ์ ์ ๋ขฐํ๋ ์ธ์ฆ๊ธฐ๊ด ์ธ์ฆ์(CA Cert) ์ถ๊ฐํ๊ธฐ ๋ฅผ ์ฐธ๊ณ ํด์ ์ธ์ฆ์๋ฅผ ์ถ๊ฐํ๋ค.
libcurl ์ ์ฌ์ฉํ ๊ฒฝ์ฐ -k ์ต์ ์ ๋ค์ 2๊ฐ์ง ์ต์ ์ ํธ์ถํ ๊ฒ๊ณผ ๋์ผ(https://github.com/lesstif/php-jira-rest-client/blob/master/src/JiraClient.php#L206-L207) ์ฐธ๊ณ
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
TLS Version ์ง์
Cookie
์ฟ ํค๋ฅผ ํ์ผ๋ก ์ ์ฅ
ํ์ผ๋ก ์ ์ฅํ ๊ฒฝ์ฐ -c, --cookie-jar ์ต์ ๋ค์ ์ฟ ํค๋ฅผ ์ ์ฅํ ํ์ผ๋ช ์ ์ ์ด์ค.
์๋ ์์ ๋ cookiejar.txt ํ์ผ์ cookie ๋ฅผ ๊ธฐ๋ก
curl -v -I -c cookiejar.txt https://www.example.com
ํ์ผ ๋๋ ๋ฌธ์์ด์์ ์ฟ ํค ์ฝ๊ธฐ
-b, --cookie ์ต์ ์ ์ฌ์ฉํ๋ฉด ์ฟ ํค๋ฅผ ์ ์ฅํ ํ์ผ์์ ์ฝ๊ฑฐ๋ ๋๋ ์ง์ ์ฟ ํค ๊ฐ์ ์ค์ ํ์ฌ HTTP ์์ฒญํ ์ ์์.
cookie-jar ์์ ์ฟ ํค ์ฝ๊ธฐ
curl -v -I -b cookiejar.txt https://www.example.com
์ฟ ํค ๊ฐ ์ค์ ํด์ ์ ์ก
tomcat ์ด ์ฌ์ฉํ๋ ์ธ์ ์ฟ ํค์ธ JSESSIONID ๋ฅผ ์ค์ ํด์ ์๋ฒ์ ์์ฒญ
curl -v -I -b "JSESSIONID=8A39E226F2B6D4DC32CE0E8D" https://www.example.com
์๋ก ์ธ์ ์ฟ ํค ์์ฑ
-b ์ต์ ๊ณผ ํจ๊ป -j, --junk-session-cookies ์ต์ ์ ์ฌ์ฉํ๋ฉด ์ฟ ํค๋ฅผ ์ ์ฅํ ํ์ผ์์ ์ธ์ ์ ๋ณด๋ง ์ฝ์ง ์์ผ๋ฏ๋ก ์๋ก ์ธ์ ์ฟ ํค๋ฅผ ์์ฑํ ์ ์์.
curl -v -I -b cookiejar.txt -j https://www.example.com
REST API ์ ์ฐ๊ณ
์์ ์ต์ ๋ค์ด ์ต์ํด์ก์ผ๋ฉด REST ๊ธฐ๋ฐ์ ์น์๋น์ค๋ฅผ ๊ฐ๋ฐํ ๋ curl ์ ์ด์ฉํด์ ํ ์คํธ ๋ฐ ๋๋ฒ๊น ์ ์ํํ ์ ์๋ค. REST API ๋ฅผ ์ ๊ณตํ๋ ์ ๋ช ํ Web App ์ ์ฐ๊ณํ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํด ๋ณธ๋ค.
curl๋ก Atlassian JIRA REST API ์ฐ๊ณํ๊ธฐ
Sonatype nexus Admin API
๊ด๋ฆฌ์ ์ํธ ๋ณ๊ฒฝ
curl -v -k -d "{"data":{"userId":"admin","oldPassword":"admin123","newPassword":"admin1234"}}" -u admin:admin123 https://my-nexus-site/nexus/service/local/users_changepw
See Also
- jq - ๋ช ๋ นํ JSON ์ฒ๋ฆฌ๊ธฐ ์ฌ์ฉ๋ฒ
- httpie - curl ์ ๋์ฒดํ http client ์ ํธ๋ฆฌํฐ
- curl ์ ์ ๋ขฐํ๋ ์ธ์ฆ๊ธฐ๊ด ์ธ์ฆ์(CA Cert) ์ถ๊ฐํ๊ธฐ
- curl windows ์์ ๋น๋ํ๊ธฐ
- RHEL/CentOS 6 ์์ curl ์ต์ ๋ฒ์ ์ค์น