跳转至

curl 命令


2013-08-24 by dongnan

举个栗子

很久很久以前可使用Google搜索:

curl -k -L -I https://www.google.com.hk

HTTP/1.1 200 OK
Date: Wed, 21 Aug 2013 10:23:54 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Big5
Set-Cookie: PREF=ID=c3e552cb49a35c5c:FF=0:NW=1:TM=1377080634:LM=1377080634:S=s3pr-FoyX2BDhP0r; expires=Fri, 21-Aug-2015 10:23:54 GMT; path=/; domain=.google.com.hk
Set-Cookie: NID=67=Sq6XV7Eos2QXN9y2bWchNvFwDjODpWF-zAVH-ngSLDH1s1GcKboQ0p-HM0mApBY9aWnHNZDqvvYqK53XC3AoiuRcnUWGV5dnVZr5dDR3ldfZz80RHMCabimSwuwU9Zbt; expires=Thu, 20-Feb-2014 10:23:54 GMT; path=/; domain=.google.com.hk; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked

curl 使用用户名密码:

curl -udongnan:ywwd.net http://localhost:88/haproxy_stats

curl 指定HTTP方法:

# 使用POST方法
curl -X POST -IL  http://ywwd.net:8000/index.php

114.242.1x.9x - - [09/Dec/2015:11:24:19 +0800] "POST / HTTP/1.1" 200 41909 "-" "curl/7.35.0" -

curl 限制带宽byte/s

# 限制 100K
curl --limit-rate 100K http://ywwd.net:8000

curl 下载文件:

# curl 默认将输出打印到标准输出中(STDOUT)中; 
curl -o /etc/yum.repos.d/epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo

curl 伪造HTTP头部:

# 伪造 HTTP_X_FORWARDED_FOR 变量
curl -k -H "HTTP_X_FORWARDED_FOR: 123.1XX.XX.XX" https://ywwd.net/ -v

# 指定多个头部
curl http://ywwd.net/ -H 'X-Forwarded-For: 1.1.1.1' -H 'X-Real-IP: 2.2.2.2'

curl 上传文件:

# 上传doc文件
curl -F "file=@test.doc" "http://your_web_server/json?token=111111"

{"ok":1,"errmsg":null,"data":{xxxxxxx}}

curl 使用标准输入:

spd-cli status | /usr/bin/curl -v -H"Content-type: application/json"  http://msg.zongming.net/post/ -X POST -d -

要在标准输入stdin中获取输入来代替文件,则在应提供文件名的位置上使用-代替。

命令参数

NAME
       curl - transfer a URL

SYNOPSIS
       curl [options] [URL...]

DESCRIPTION
       curl  is  a tool to transfer data from or to a server, using one of the supported protocols 
       (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP,
       SMTP, SMTPS, TELNET and TFTP).  
       The command is designed to work without user interaction.


   -u, --user <user:password>
          Specify the user name and password to use for server authentication. Overrides -n, --netrc and --netrc-optional.

   -X, --request <command>
          (HTTP) Specifies a custom request method to use when communicating with the HTTP server.  
          The specified request will be used instead of the method otherwise used (which defaults to GET). 
          Read the HTTP 1.1 specification for details and explanations. Common additional HTTP requests 
          include PUT and DELETE, but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more.

   --limit-rate <speed>
          Specify the maximum transfer rate you want curl to use. This feature is useful if you have a 
          limited pipe and you'd like your transfer not to use your entire bandwidth.

   -o, --output <file>
          Write  output  to <file> instead of stdout. If you are using {} or [] to fetch multiple documents, 
          you can use '#' followed by a number in the <file> specifier. That variable will be replaced
          with the current string for the URL being fetched.

   -k, --insecure
          (SSL)  This option explicitly allows curl to perform "insecure" SSL connections and transfers. 
          All SSL connections are attempted to be made secure by using the CA certificate bundle installed
          by default. This makes all connections considered "insecure" fail unless -k, --insecure is used.

   -H, --header <header>
          (HTTP) Extra header to use when getting a web page. You may specify any number of extra headers. Note that if you should add a custom header that has the same name as one of the internal ones
          curl would use, your externally set header will be used instead of the internal one. This allows you to make even trickier stuff than curl would normally do. You should not replace internally
          set headers without knowing perfectly well what you're doing. Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H "Host:". If you  send
          the custom header with no-value then its header must be terminated with a semicolon, such as -H "X-Custom-Header;" to send "X-Custom-Header:".



回到页面顶部