必须推荐给基友的Curl工具使用指南
source link: http://www.androidchina.net/12629.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Curl工具使用指南
curl
是一个命令行工具,用于发送客户端请求。发送客户端请求大家常用的可能是类似postman
等工具,但是为什么要使用curl
呢?curl
等最大优势在于随时随手可以发送,非常方便。比如很多场景下我们只是想快速验证一个请求或接口:
# 直接在命令行发送一个GET请求
curl https://xxx.com/api/v1/xxx
# 发送POST请求
curl -X POST -d "k1=123&k2=456" https://xxx.com/api/v1/xxx
curl安装
curl
的安装就是到官网根据你的系统下载对应的版本进行安装,但是安装好后要配置环境变量。
官网: https://curl.se/download.html
# 安装好后重启终端运行,查看版本
curl -V
如果能看到如下内容则是安装成功了:
发送GET请求
-
curl
后面直接添加url地址即可访问GET
请求
curl https://www.baidu.com
请求百度网址的效果如下:
-
发送
GET
请求携带请求参数
curl https://www.xxx.com/?key=value1&key2=value2
POST请求
-X POST --data "k1=v1&k2=v2"
发送post请求,并且携带请求数据。下面演示一个接收POST
请求并返回POST
数据的的Node
服务和CURL
发送POST
请求示例:
/**
* http服务,处理post请求并将post数据返回
*/
const http = require('http');
const server = http.createServer((req, res) => {
if (req.method === 'POST' && hasbody(req)) {
const buffer = [];
req.on('data', (chunk) => {
buffer.push(chunk);
});
req.on('end', () => {
const rawBody = Buffer.concat(buffer).toString();
res.writeHead(200);
res.end(rawBody);
});
} else {
res.end('');
}
});
server.listen(3000, () => {
console.log('server running at port 3000.');
});
// 判断是否有body请求实体数据
function hasbody (req) {
return req.headers['transfer-encoding'] !== undefined ||
!isNaN(req.headers['content-length']);
}
curl
发送post
请求:
curl -X POST --data "key1=123&key2=456" http://localhost:3000
# --data可以简写为-d
curl -X POST -d "key1=123&key2=456" http://localhost:3000
-
对
post
数据进行url
编码
# 注意,--data-urlencode的简写不是-d
# --data的简写是-d
curl --data-urlencode "k1=1&k2=a b" http://localhost:3000
例如下面发送对请求数据中,a
和b
之间有个空格,使用--data-urlencode
会对其进行encodeURIComponent
编码:
发送HEAD请求
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200);
res.end();
});
server.listen(3000, () => {
console.log('server running at port 3000.');
});
-I
参数可以发送HEAD
请求:
curl -I http://localhost:3000
发送DELETE请求
-X DELETE
参数可以发送DELETE
请求:
curl -X DELETE http://localhost:3000
发送PUT请求
下面起一个非常简易的node
服务,将用户上传的图片保存成图片:
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
// 如果是PUT请求且访问的接口是/upload/file
if (req.method === 'PUT' && req.url === '/upload/file') {
// 将用户的数据存成图片static/mime.png图片
const writeStream = fs.createWriteStream(__dirname + '/static/mime.png');
req.pipe(writeStream).on('finish', () => {
res.writeHead(200);
res.end('upload success');
});
writeStream.on('error', (err) => {
res.writeHead(500);
res.end('server error.');
});
}
});
server.listen(3000, () => {
console.log('server running at port 3000.');
});
利用curl
的-T
可以发送PUT
类型请求,同时需要指定上传的资源路径:
curl -T ./mime.png http://localhost:3000/upload/file
同时请求结束后,可以看到上传的图片已经被保存下来了:
curl下载文件
下载保存文件是加-o 保存地址
参数。
# 将baidu的html文件下载到本地
curl -o ./my-download.html https://www.baidu.com
curl
命令执行的效果如下图,而且文件也已经被下载了下来:
查看响应头参数
-i
参数可以返回响应头信息
curl -i https://www.baidu.com
查看完整的报文信息
-
-v
参数查看完整的报文信息
curl -v https://www.baidu.com
-
--trace ./log.txt
查看更详细的信息并将数据写入到指定文件中。
curl --trace ./log.txt https://www.baidu.com
-
--trace-ascii ./log.txt
以ascii编码格式查看更详细的信息并将数据写入到指定文件中。
指定请求的user-agent
我们启动一个最简单的http
服务,并且配置好vscode
的debug
用于我们查看curl
发送的信息:
const http = require('http');
const server = http.createServer((req, res) => {
// 在此处打上断点,查看req请求对象
res.end('');
});
server.listen(3000, () => {
console.log('server running at port 3000.');
});
如果直接通过curl
发送GET
请求,可以看到user-agent
为:
chrome`浏览器的`user-agent`是`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36
--user-agent "xxx"
可以指定发送请求时的user-agent
,参数简写为-A
:
curl --user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36" http://localhost:3000
# --user-agent简写为-A
curl -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36" http://localhost:3000
指定请求的跳转
--referer 跳转前url 跳转后url
curl --referer http://localhost:3000 http://localhost:3000/newpath
此时发送的请求的req.url
是新的地址http://localhost:3000/newpath
,并且headers
中携带了referer
字段。
请求时携带cookie参数
-
--cookie "k1=v1&k2=v2"
携带cookie
参数,--cookie
可以简写为-b
:
curl --cookie "k=1&k2=2" http://localhost:3000
# --cookie简写为-b
curl -b "k=1&k2=2" http://localhost:3000
-
curl
保存服务端的cookie
到指定文件
为了使用curl
时能携带服务端设置的cookie
,我们可以先把服务的cookie
存到本地,然后后续使用curl
的适合再携带上。如下,有一个node
设置cookie
的例子:
const http = require('http');
const server = http.createServer((req, res) => {
// Node设置cookie
res.writeHead(200, {
'Set-Cookie': 'key1=value1&key2=value2',
});
res.end('cookie set success.');
});
server.listen(3000, () => {
console.log('server running at port 3000.');
});
通过curl
发送请求携带-c path/to/save
,可以将node
设置的cookie
保存到本地:
curl -c ./cookie http://localhost:3000
-
curl
发送请求时携带cookie
文件
curl -b ./cookie http://localhost:3000
此时debug
可以看到req
上已经携带了我们的cookie
:
- curl文档 :https://catonmat.net/cookbooks/curl
- curl网站开发指南 阮一峰 :https://www.ruanyifeng.com/blog/2011/09/curl.html
- curl 的用法指南 阮一峰 :https://www.ruanyifeng.com/blog/2019/09/curl-reference.html
curl
对于想随手快速验证一个服务是非常便利的,而且真是越用越好用。希望小伙伴看完这篇教程之后能够使用起来,帮助到大家平日的开发工作。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK