CURL:Examples
cURL을 사용한 예제들.
Simple Usage
Get the main page from a web-server:
Get a README file from an FTP server:
Get a web page from a server using port 8000:
Get a directory listing of an FTP site:
Get the all terms matching curl from a dictionary:
Get the definition of curl from a dictionary:
Fetch two documents at once:
Get a file off an FTPS server:
or use the more appropriate FTPS way to get the same file:
Get a file from an SSH server using SFTP:
Get a file from an SSH server using SCP using a private key (not password-protected) to authenticate:
Get a file from an SSH server using SCP using a private key (password-protected) to authenticate:
Get the main page from an IPv6 web server:
Get a file from an SMB server:
Uploading
FTP / FTPS / SFTP / SCP
Upload all data on stdin to a specified server:
Upload data from a specified file, login with user and password:
Upload a local file to the remote site, and use the local file name at the remote site too:
Upload a local file to get appended to the remote file:
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:
SMB / SMBS
HTTP
Upload all data on stdin to a specified HTTP site:
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
Request Header
여러 개의 헤더를 적용할 경우 아래와 같이 전송하면 된다.
헬스 체크
플래그 정보:
-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:
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
를 사용하자.
쿠키 정보 함께 보내기
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에서 캡쳐 안 되는 문제가 발생될 경우 아래와 같은 방법으로 해결할 수 있다.
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.txt
http://www.test.co.kr/testpage
- GET방식 데이터 요청.
:
- 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 -v
http://www.joinc.co.kr
웹페이지의 타이틀 정보를 추출 하는 방법
FTP 관련 예제
- FTP의 Child Node를 출력한다.
- 중요한 것은 경로의 마지막에 슬래시(
/
)를 붙여야 한다는 점이다. -
curl
ftp://10.10.1.55/dstdir/
- FTP로 파일을 다운로드받을 경우 아래와 같이 사용한다.
-
curl -O
ftp://10.10.1.55/dstdir/tmp.txt
- FTP로 파일을 업로드시에는 간단하게 다음과 같이 사용할 수 있다.
- 중요한 것은 경로의 마지막에 슬래시(
/
)를 붙여야 한다는 점이다. -
curl -T ~/tmp.txt
ftp://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하여 실행한 후, 에러메시지만 처리할 경우 다음 옵션을 사용할 수 있다.
-
curl
ftp://10.10.1.55/dstdir/
-s -S
- 동작시간에 타임아웃을 설정할 경우 다음 옵션을 사용할 수 있다.
-
curl
ftp://10.10.1.55/dstdir/
-m 5
- 연결시간에 타임아웃을 설정할 경우 다음 옵션을 사용할 수 있다.
-
curl
ftp://10.10.1.55/dstdir/
--connect-timeout 3
- Remote server에 디렉토리를 자동으로 생성하고자 할 경우 다음옵션을 줄 수 있다.
-
curl
ftp://10.10.1.55/dstdir/
--ftp-create-dirs
- 입력범위를 줄 경우 아래와 같이 대괄호(
[]
)를 사용하여 범위를 지정한다. -
curl -O
ftp://ftp.numericals.com/file[1-100].txt
- 범위를 사용할 경우 간격을 조절할 경우 아래와 같이 콜론(
:
)을 사용한다. -
curl -O
ftp://ftp.numericals.com/file[1-100:10
].txt
- 수치범위에
0
을 포함할 경우 알래와 같이 사용한다. -
curl -O
ftp://ftp.numericals.com/file[001-100].txt
- 범위를 사용할 경우 알파벳도 허용된다.
-
curl -O
ftp://ftp.letters.com/file[a-z].txt