Curl工具使用指南

好用的小火箭节点推荐⭐Shadowrocket高速节点推荐

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可以简写为-dcurl -X POST -d "key1=123&key2=456" http://localhost:3000

对 post 数据进行 url 编码

# 注意,--data-urlencode的简写不是-d# --data的简写是-dcurl --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简写为-Acurl -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 跳转后urlcurl --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简写为-bcurl -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

版权声明:
作者:shadowrocket
链接:https://www.shadowrockets.wang/600.html
来源:Shadowrocket官网
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>