Skip to content

CURL:Examples

cURL을 사용한 예제들.

Simple Usage

Get the main page from a web-server:

curl https://www.example.com/

Get a README file from an FTP server:

curl ftp://ftp.funet.fi/README

Get a web page from a server using port 8000:

curl http://www.weirdserver.com:8000/

Get a directory listing of an FTP site:

curl ftp://ftp.funet.fi

Get the all terms matching curl from a dictionary:

curl dict://dict.org/m:curl

Get the definition of curl from a dictionary:

curl dict://dict.org/d:curl

Fetch two documents at once:

curl ftp://ftp.funet.fi/ http://www.weirdserver.com:8000/

Get a file off an FTPS server:

curl ftps://files.are.secure.com/secrets.txt

or use the more appropriate FTPS way to get the same file:

curl --ftp-ssl ftp://files.are.secure.com/secrets.txt

Get a file from an SSH server using SFTP:

curl -u username sftp://example.com/etc/issue

Get a file from an SSH server using SCP using a private key (not password-protected) to authenticate:

curl -u username: --key ~/.ssh/id_rsa scp://example.com/~/file.txt

Get a file from an SSH server using SCP using a private key (password-protected) to authenticate:

curl -u username: --key ~/.ssh/id_rsa --pass private_key_password
scp://example.com/~/file.txt

Get the main page from an IPv6 web server:

curl "http://[2001:1890:1112:1::20]/"

Get a file from an SMB server:

curl -u "domain\username:passwd" smb://server.example.com/share/file.txt

Uploading

FTP / FTPS / SFTP / SCP

Upload all data on stdin to a specified server:

curl -T - ftp://ftp.upload.com/myfile

Upload data from a specified file, login with user and password:

curl -T uploadfile -u user:passwd ftp://ftp.upload.com/myfile

Upload a local file to the remote site, and use the local file name at the remote site too:

curl -T uploadfile -u user:passwd ftp://ftp.upload.com/

Upload a local file to get appended to the remote file:

curl -T localfile -a ftp://ftp.upload.com/remotefile

Curl also supports ftp upload through a proxy, but only if the proxy is configured to allow that kind of tunneling. If it does, you can run curl in a fashion similar to:

curl --proxytunnel -x proxy:port -T localfile ftp.upload.com

SMB / SMBS

curl -T file.txt -u "domain\username:passwd" smb://server.example.com/share/

HTTP

Upload all data on stdin to a specified HTTP site:

curl -T - http://www.upload.com/myfile

Note that the HTTP server must have been configured to accept PUT before this can be done successfully.

For other ways to do HTTP data upload, see the POST section below.

Resume Broken Download

curl -C - url
## OR
curl -L -O -C - url
## OR
curl -L -o 'filename-here' -C - url

Request Header

여러 개의 헤더를 적용할 경우 아래와 같이 전송하면 된다.

$ curl -H "Content-Type: application/json" -H "Accept: application/json" http://127.0.0.1/

헬스 체크

curl -sSf http://example.org > /dev/null

플래그 정보:

  -s/--silent
     Silent or quiet mode. Do not show progress meter or error messages.  
     Makes Curl mute.

  -S/--show-error
     When used with -s it makes curl show an error message if it fails.

  -f/--fail
     (HTTP)  Fail silently (no output at all) on server errors. This is mostly
     done to better enable scripts etc to better deal with failed attempts. In
     normal  cases  when a HTTP server fails to deliver a document, it returns
     an HTML document stating so (which often also describes  why  and  more).
     This flag will prevent curl from outputting that and return error 22.

     This method is not fail-safe and there are occasions where non-successful
     response codes will  slip  through,  especially  when  authentication  is
     involved (response codes 401 and 407).

Used POST method

POST 방식의 경우 메시지를 body에 담아서 보내야 한다. -d, --data 옵션을 이용해서 데이터를 보낼 수 있다. 혹은 파일의 내용을 POST 데이터로 보낼 수도 있다. Syntax:

$ curl -X POST -d '내용' 타겟URL
$ curl -X POST -d @파일명  타겟URL

Example:

$ curl -d "name=yundream&age=19&address=Seoul ... ..." http://www.joinc.co.kr/curltest.php
$ curl -X POST http://192.168.57.3:5984/mywiki/ -d @curl.json -H "Content-Type: application/json"

File Data

파일을 사용하여 Content 정보에 바이너리를 추가하고 싶다면 --data-binary를 사용하면 된다. 단, 파일명 앞에 @를 꼭 붙여야 한다.

$ curl -u user:pass -X POST -H "Content-Type: multipart/form-data" --data-binary "@package.deb" http://nexus_url:8081/repository/repo_name/

SFTP

SFTP 파일 업로드:

$ curl -T file.txt sftp://user:[email protected]:36651/~/
$ curl -T file.txt scp://user:[email protected]:36651/home/chanik/

Protocol debugging

--verbose--trace, --trace-ascii를 이용해서 HTTP 데이터 통신 과정에 대한 상세정보를 얻을 수 있다. --verbose로 불충분하다면 --trace-ascii를 사용하자.

$ curl --trace-ascii debug.dump http://www.test.co.kr/testpage

쿠키 정보 함께 보내기

Cookie정보를 함께 보내는 방법은 아래와 같다.

curl -b "ASP.NET_SessionId=d8fcb899-c453-4688-8bb1-f1d4993bda5b"  http://mobile.zekitalk.com/typhoon/Typhoon_ListEvent.aspx

Recursive download?

재귀적으로 웹페이지를 다운받을 수 있는 기능이 있는가? 결론은 wget을 사용해야 한다. 관련 문구는 아래와 같다.

Single shot.
curl is basically made to do single-shot transfers of data. It transfers just the URLs that the user specifies, and does not contain any recursive downloading logic nor any sort of HTML parser.

Proxy server

Proxy서버의 사용여부는 -x옵션을 사용하면 된다. cURL 사용시 Fiddler에서 캡쳐 안 되는 문제가 발생될 경우 아래와 같은 방법으로 해결할 수 있다.

curl -x 192.168.0.50:8888 http://mobile.zekitalk.com/typhoon/Typhoon_ListEvent.aspx

Make directory using curl on FTP

curl -p - --insecure  "ftp://82.45.34.23:21/CurlPutTest/test" --user "testuser:testpassword" -Q "-MKD /CurlPutTest/test"  --ftp-create-dirs

Make directory using curl on SFTP

curl  -k "sftp://83.46.38.23:22/CurlPutTest/test " --user "testuser:testpassword" -Q "–MKDIR /CurlPutTest/Test" --ftp-create-dirs

HTTP 관련 예제

헤더를 포함해여 출력한다.
curl –i [URL]
헤더만 출력한다.
curl –I [URL]
헤더를 파일로 출력한다.
curl –D [FILE] [URL]
디버깅 정보를 파일로 저장.
curl --trace-ascii DebugDump.txthttp://www.test.co.kr/testpage
GET방식 데이터 요청.

:

# curl http://www.joinc.co.kr
# curl http://www.joinc.co.kr?action=del&id=12345
POST방식 데이터 요청.
-d, --data옵션을 이용해서 데이터를 보낼 수 있다. 파일명 앞에 @를 붙이면 파일의 내용을 POST 데이터로 보낼 수도 있다.

:

# curl -d "name=yundream&age=19&address=Seoul ... ..." http://www.joinc.co.kr/curltest.php
# curl -X POST http://192.168.57.3:5984/mywiki/ -d @curl.json -H "Content-Type: application/json"
상세정보출력.
curl -vhttp://www.joinc.co.kr

웹페이지의 타이틀 정보를 추출 하는 방법

curl -s "https://news.hada.io/topic?id=10569" | grep -oP '(?<=<title>)(.*)(?=</title>)'

FTP 관련 예제

FTP의 Child Node를 출력한다.
중요한 것은 경로의 마지막에 슬래시(/)를 붙여야 한다는 점이다.
curlftp://10.10.1.55/dstdir/
FTP로 파일을 다운로드받을 경우 아래와 같이 사용한다.
curl -Oftp://10.10.1.55/dstdir/tmp.txt
FTP로 파일을 업로드시에는 간단하게 다음과 같이 사용할 수 있다.
중요한 것은 경로의 마지막에 슬래시(/)를 붙여야 한다는 점이다.
curl -T ~/tmp.txtftp://10.10.1.55/dstdir/
globbing을 사용한 파일 업로드
curl -T 'image[1-99].jpg'ftp://ftp.example.com/upload/
여러 파일 업로드 방법
curl -T '{file1,file2}'ftp://ftp.example.com/upload/
curl -T '{Huey,Dewey,Louie}.jpg'ftp://ftp.example.com/nephews/
스크립트나, 프로그램상에서 cURL을 invoke하여 실행한 후, 에러메시지만 처리할 경우 다음 옵션을 사용할 수 있다.
curlftp://10.10.1.55/dstdir/-s -S
동작시간에 타임아웃을 설정할 경우 다음 옵션을 사용할 수 있다.
curlftp://10.10.1.55/dstdir/-m 5
연결시간에 타임아웃을 설정할 경우 다음 옵션을 사용할 수 있다.
curlftp://10.10.1.55/dstdir/--connect-timeout 3
Remote server에 디렉토리를 자동으로 생성하고자 할 경우 다음옵션을 줄 수 있다.
curlftp://10.10.1.55/dstdir/--ftp-create-dirs
입력범위를 줄 경우 아래와 같이 대괄호([])를 사용하여 범위를 지정한다.
curl -Oftp://ftp.numericals.com/file[1-100].txt
범위를 사용할 경우 간격을 조절할 경우 아래와 같이 콜론(:)을 사용한다.
curl -Oftp://ftp.numericals.com/file[1-100:10].txt
수치범위에 0을 포함할 경우 알래와 같이 사용한다.
curl -Oftp://ftp.numericals.com/file[001-100].txt
범위를 사용할 경우 알파벳도 허용된다.
curl -Oftp://ftp.letters.com/file[a-z].txt

See also

Favorite site