指定用户代理

1
2
# -A, --user-agent <name> Send User-Agent <name> to server
curl -A "Mozilla/5.0" http://localhost:8080/validation/basic

设置参照页(referer)

1
2
# -e, --referer <URL> Referrer URL
curl --referer http://google.com http://knopper.org

使用 cURL 进行认证

HTTP Basic 认证

  1. 使用 Authorization 头部
1
2
3
curl -H "Authorization: Basic Z3Vlc3Q6Z3Vlc3Q=" http://localhost:8080/validation/basic

# Authorization Success
  1. 使用 -u 完成 HTTP 或 FTP 认证
1
2
3
4
5
# curl -u user:pass
curl -u "guest:guest" http://localhost:8080/validation/basic

# 提示后输入密码
curl -u guest http://localhost:8080/validation/basic

只打印返回头部信息

1
2
3
curl -I http://localhost:8080/validation/basic

curl --head http://localhost:8080/validation/basic

打印响应头部信息

1
2
# -i, --include       Include protocol response headers in the output
curl -i http://localhost:8080/validation/basic

使用其他 HTTP 方法

1
2
#  -X, --request <command> Specify request command to use
curl -X DELETE http://upload.linuxhandbook.org/files/deleteFile.txt

发送数据到服务器

1
2
# -d, --data <data>   HTTP POST data
curl -i -X POST http://localhost:8080/addPerson -d '{"name":"Jason","age":26,"gender":"male","address"AVE.18"}' -H "Content-Type: application/json"
1
2
#  -c, --cookie-jar <filename> Write cookies to <filename> after operation
curl -c googleCookie.txt http://www.google.com/files

使用Cookie

1
2
3
4
5
6
7
#  -b, --cookie <data> Send cookies from string/file
# 从文件读取或者在命令行中指定

# 多个 cookie 使用分号分割
curl http://example.com --cookie "user=username;pass=hack"

curl -b googleCookie.txt http://www.google.com/files

开启新的会话(Session)

1
2
# -j, --junk-session-cookies Ignore session cookies read from file
curl -b googleCookie.txt http://www.google.com/files -j