---
title: "curl 介绍和基本用法"
author: "lihaiming"
date: "2017-09-20T11:54:36Z"
tags: []
categories: ["操作系统"]
slug: "curl"
draft: false
lastmod: "2017-09-20T11:54:36Z"
id: 2393845
url: "https://lihaiming.iteye.com/blog/2393845"
---

curl是一个广泛使用的用来上传和下载的命令行工具，当然严格来讲，它还可以有别的用途。对于测试来讲，它是Web相关测试非常实用的工具，包括debugging，使用起来非常方便。而且另一方面，因为它是纯命令行的工具，所以也可以非常方便的作为一个组件集成到automation或者其他的测试框架里面，将HTTP/HTTPS/FTP相关的上传和下载等任务交给它。

最近因为在做web service相关的测试，用到了curl，觉得还不错，顺便给大家介绍一下。

 

official site: [http://curl.haxx.se/](http://curl.haxx.se/) ， 目前最新的版本是7.20.0。

**基本的用法示例**

curl http://www.google.com

curl -o f1 [ftp://user:pwd@myftp/Ricky/test.ini](ftp://user:pwd@myftp/Ricky/test.ini)

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

**下面主要介绍一下常用的参数，关于这方面，可以查看它自带的帮助文档。**

**-V show the version of curl**

C:/Tools/curl-7.20.0&gt;curl -V

curl 7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3

Protocols: **dict file ftp ftps http https imap imaps ldap pop3 pop3s rtsp smtp smtps telnet tftp**

Features: AsynchDNS Largefile NTLM SSL SSPI libz

**-o \[filename], save content to a file**

**-O use the same name**

curl -o g.html www.google.com

curl -O http://www.google.com/intl/en\_ALL/images/logo.gif

**-i, Include the HTTP-header in the output**       
curl -i http://www.google.com

**-u username:password**

curl -u user:pwd [ftp://myftp/Ricky/test.ini](ftp://myftp/Ricky/test.ini)

 

**-d parameters**

curl http://www.yahoo.com/login.cgi?user=nick&password=12345

curl -d "user=nick&password=12345" http://www.yahoo.com/login.cgi

**-x  proxy\_ip:port, access with proxy**

curl -i -x myproxy:8080 -o google\_proxy.html http://www.google.com

 

*HTTP/1.1 302 Found*

*Via: 1.0 myproxy*

*Content-Length: 222*

*Date: Mon, 22 Mar 2010 14:15:48 GMT*

*Location: http://www.google.com.tw/*

*Content-Type: text/html; charset=UTF-8*

*Cache-Control: private*

*Set-Cookie: PREF=ID=aa97c3734c862ddf:TM=1269267348:LM=1269267348:S=4FEEGAPNu2FN2lho; expires=Wed, 21-Mar-2012 14:15:48 GMT; path=/; domain=.google.com*

*Set-Cookie: NID=32=MXKkZjgjiNGVNnPv9w384COa2KJZOLu5v9\_5coX\_N1tJtaa97-dJUxb7DCz90vQsm0fLEPg0Ee3nXv1yDDcE3ZN0sOu7mq9-mjB9CL0okXGjiIvox2FTcw0HBV7hBaLC; expires=Tue, 21-Sep-2010 14:15:48 GMT; path=/; domain=.google.com; HttpOnly*

*Server: gws*

*X-XSS-Protection: 0*

 

*&lt;HTML&gt;&lt;HEAD&gt;&lt;meta http-equiv="content-type" content="text/html;charset=utf-8"&gt;*

*&lt;TITLE&gt;302 Moved&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;*

*&lt;H1&gt;302 Moved&lt;/H1&gt;*

*The document has moved*

*&lt;A HREF="http://www.google.com.tw/"&gt;here&lt;/A&gt;.*

*&lt;/BODY&gt;&lt;/HTML&gt;*

 

**-r \[start-end], such as "-r 0-1024"**

curl -i -r 0-1024 -o rfc1.txt http://xml.resource.org/public/rfc/bibxml3/rdf/item.I-D.6man-pmip6-ind.rdf

*header:*

*HTTP/1.1 *206 Partial Content**

*Date: Mon, 22 Mar 2010 13:50:46 GMT*

*Server: Apache/2.2.15 (Debian)*

*Last-Modified: Mon, 09 Mar 2009 23:02:11 GMT*

*ETag: "148be6-466-464b79fe1a2c0"*

*Accept-Ranges: bytes*

*Content-Length: 1025*

***Content-Range: bytes 0-1024/1126***

*Content-Type: application/rdf+xml*

**note**: not all web server support partial content, or it will response all even you try to get part

curl -i -r 0-1024 http://www.sina.com.cn -o sina\_part1.htm

it will give you all the page.

**-k insecure SSL , will not check the cert**

curl -k https://ip:8445

 

**-T upload a file with FTP**

curl -T curl.html -u user:pwd [ftp://myftp/Ricky/](ftp://myftp/Ricky/)

execute once again will overwrite

**-F/--form &lt;name=content&gt;, use for http post**

curl -F password=@/etc/passwd www.mypasswords.com

You can also tell curl what Content-Type to use by using 'type=', in a manner similar to: 

curl -F "web=@index.html;type=text/html" url.com  

 

最后顺便说一下，如果是用来做HTTP的测试和调试，希望有GUI的话，也有不少其它的工具可以选择，包括Fiddler，JMeter和Firefox的tamper data插件。

http://blog.csdn.net/superqa/article/details/5418636