# 우분투
$ sudo apt update
$ sudo apt install curl
# CentOS
$ sudo yum install curl
https://curl.se/windows/
<- 여기서 설치!!
$ curl [options...] <url>
https 프로토콜에서 SSL 인증서에 대한 검증없이 연결
HTTP 헤더만 보여주고 컨텐츠는 표시하지 않음
<file>
HTTP 헤더를 file에 기록 (덤프)
HTTP POST 요청 데이터 입력
동작하면서 세세한 내용을 출력
헤더에 있는 파일 이름으로 다운로드 파일을 저장
curl로 받아온 내용을 FILE이라는 이름의 파일로 저장
파일 저장시 리모트에 저장되어 있던 이름을 그대로 가져와서 로컬에 저장
요청 시 사용할 메소드의 종류(GET, POST, PUT, PATCH, DELETE)
응답에 Content만 출력하지 않고 서버의 Reponse도 포함해서 출력한다(디버깅에 유용)
서버에 User-Agent<name>
보내기
서버 사용자 및 비밀번호
로컬 FILE을 대상으로 전송
HTTP 오류 시 자동으로 실패(출력 없음)
전송할 사이트 url 및 ip 주소
전송할 헤더를 지정
어떤 웹서비스는 파일 다운로드시 Content-Disposition Header를 파싱해야 정확한 파일이름을 알 수 있을 경우가 있다. -J 옵션을 주면 헤더에 있는 파일 이름으로 저장한다.
파일 다운로드 재개
$ curl www.example.com
$ curl -X GET www.example.com
# GET방식은 Body 없음
# -X 요청메소드 종류 명시
# url 형식 데이터
$ curl -d "key1=value1&key2=value2" \ # -d 옵션으로 body 데이터를 기재
-H "Content-Type: application/x-www-form-urlencoded" \ # -H 옵션으로 전송할 헤더 지정 (디폴트 : application/x-www-form-urlencoded)
-X POST http://localhost:8000/data # -X 옵션으로 POST 메소드 지정하고 요청할 url명시
# JSON 형식 데이터
$ curl -d '{"key1":"value1", "key2":"value2"}' \
-H "Content-Type: application/json" \
-X POST http://localhost:8000/data
# 파라미터값을 파일로 지정해서 보내기
curl -d "@data.txt" -X POST http://localhost:3000/data
curl -d "@data.json" -X POST http://localhost:3000/data
$ curl -X PUT -d 'name=inpa&email=inpa@gmail.com' http://localhost:8080/user/100
$ curl -X PUT -H "Content-Type: application/json" -d '{"name":"inpa","email":"inpa@gmail.com"}' http://localhost:8080/user/100
# 파일명으로 PUT
$ curl -T filename.txt http://www.example.com/dir/
$ curl -X DELETE http://localhost:8080/user/100
$ curl -F upload=@파일명 -F press=OK www.xxx.com/blog/post.cgi
# gettext.html 페이지가 mygettext.html 이라는 파일에 저장
$ curl -o mygettext.html http://www.gnu.org/software/gettext/manual/gettext.html
# 로컬 시스템에 gettext.html 이라는 파일 자체에 내용이 저장
$ curl -O http://www.gnu.org/software/gettext/manual/gettext.html
# 출력 재지정 연산자로 파일 저장
$ curl http://www.gnu.org/software/gettext/manual/gettext.html > mygettext.html
$ curl -O URL1 -O URL2
$ curl -O http://mirrors.edge.kernel.org/archlinux/iso/2018.06.01/archlinux-2018.06.01-x86_64.iso \
-O https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso
# 범위 지정 - foo0.txt 부터 foo9.txt 파일 다운 받기
$ curl -O http://example.com/foo[0-9].txt
# []는 여러 번 조합가능하고, foo-a0 ~ foo-z9까지의 파일 다운
$ curl -O http://example.com/foo-[a-z][0-9].txt
# 중괄호 {}를 써서 foo.txt, bar.txt, baz.txt를 다운
$ curl -O http://example.com/{foo,bar,baz}.txt
$ files="foo bar baz" # files 라는 환경변수에 파일명 할당
$ for name in $files; do
> curl -O "http://example.com/${name}.txt"
> done
# => files에 파일명 할당하고 for문으로 순회하면서 ${name}에 값 전달하여 다운
$ curl -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso
# ... 다운로드 중
# 갑자기 인터넷이 불안정 하여 연결이 끊김
$ curl -C - -O http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso
# 다운로드 재개
# 데이터 전송을 초당 1000 바이트로 제한
$ curl --limit-rate 1000B -O http://www.gnu.org/software/gettext/manual/gettext.html
# 다운로드 속도를 1MB로 제한
$ curl --limit-rate 1m -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
$ curl -u username:password URL
# 로그인하면 사용자의 홈 디렉토리에 있는 모든 파일과 디렉터리가 나열
$ curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/
# FTP 서버에서 단일 파일을 다운로드
$ curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.tar.gz
# -T 옵션을 사용해 파일을 FTP 서버에 업로드
$ curl -T newfile.tar.gz -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/
# 범위를 사용하여 나열/다운로드. 범위가 지정되면 범위 내에서 일치하는 파일이 다운로드. 패키지 다운로드 할때 유용
$ curl ftp://ftp.uk.debian.org/debian/pool/main/[a-z]/
# 헤더정보 보기 -I
$ curl -I google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.co.kr/?gfe_rd=cr&ei=NKoMV5TZCILD8AfCsqToCw
Content-Length: 261
Date: Tue, 12 Apr 2016 07:56:36 GMT
# 헤더 + 본문 보기 -i
$ curl -i google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.co.kr/?gfe_rd=cr&ei=gqoMV9mPLczU8AefxoxA
Content-Length: 259
Date: Tue, 12 Apr 2016 07:57:54 GMT
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.kr/?gfe_rd=cr&ei=gqoMV9mPLczU8AefxoxA">here</A>.
</BODY></HTML>
$ curl -v www.example.com
$ curl -e http://리퍼러주소 daniel.haxx.se
# Firefox 60을 에뮬레이트하는 경우
curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" https://getfedora.org/
요청된 웹페이지가 다른 위치로 이동하면 HTTP 위치 헤더가 응답으로 전송되고 실제 웹페이지가 있는 위치가 포함됨.
예를 들어, 누군가가 한국에서 브라우저에 google.com을 입력하면 자동으로 google.co.kr로 리다이렉션 되는데, 아래와 같이 HTTP 위치 헤더를 기반으로 수행되게 된다.
$ curl http://www.google.com
<TITLE>302 Moved</TITLE>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.kr/">here</A>
위의 출력을 요청된 문서가 http://www.google.co.kr로 이동되었음을 나타낸다.
따라서, -L 옵션을 사용하여 리다이렉션을 따르도록 curl을 사용할 수 있다.
$ curl -L http://www.google.com # 리다이렉트 된 google.co.kr 의 html 소스 코드를 다운로드함.
# Cookie key=value 쌍을 서버로 전송
curl -b "name=inpa" www.cookiesite.com
# headers_and_cookies 파일로 헤더 정보다 모두 저장
# 헤더 정보에는 쿠키 정보도 포함되어 있다.
$ curl -D headers_and_cookies www.cookiesite.com
# stored_cookies 파일에 저장된 쿠키 내용을 서버로 전송
$ curl -b stored_cookies www.cookiesite.com
# 서버에서 전송한 쿠키를 리다이렉트하면서 서버측에 재전송
$ curl -b "name=inpa" -L www.cookiesite.com
# -b cookies.txt 는 이미 존재하는 쿠키 파일(Mozilla 호환)을 읽어서 서버에 전송
# -c newcookies.txt 는 새로 새성된 쿠키를 파일로 저장
$ curl -b cookies.txt -c newcookies.txt www.cookiesite.com
# 192.168.44.1 포트 8888에서 프록시를 사용하여 지정된 리소스를 다운로드
$ curl -x 192.168.44.1:8888 http://linux.com/
# 서버에 인증이 필요한 경우 -U(--proxy-user) 옵션을 사용하고 사용자 이름과 암호를 콜론(user:password)으로 구분
$ curl -U username:password -x 192.168.44.1:8888 http://linux.com/
curl smtp://mail.example.com --mail-from myself@example.com --mail-rcpt \
receiver@example.com --upload-file email.txt
$ curl ifconfig.me
$ curl http://wttr.in/Seou
$ curl http://wttr.in/Moon@연도-월-일
$ curl http://wttr.in/Moon@2022-06-17