必须推荐给基友的Curl工具使用指南

必须推荐给基友的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安装

curl的安装就是到官网根据你的系统下载对应的版本进行安装,但是安装好后要配置环境变量。

# 安装好后重启终端运行,查看版本
curl -V
复制代码

如果能看到如下内容则是安装成功了:

必须推荐给基友的Curl工具使用指南

发送GET请求

curl https://www.baidu.com
复制代码

请求百度网址的效果如下:

必须推荐给基友的Curl工具使用指南

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
复制代码
必须推荐给基友的Curl工具使用指南

# 注意,--data-urlencode的简写不是-d
# --data的简写是-d
curl --data-urlencode "k1=1&k2=a b"  http://localhost:3000
复制代码

例如下面发送对请求数据中,ab之间有个空格,使用--data-urlencode会对其进行encodeURIComponent编码:

必须推荐给基友的Curl工具使用指南

发送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
复制代码
必须推荐给基友的Curl工具使用指南

发送DELETE请求

-X DELETE参数可以发送DELETE请求:

curl -X DELETE http://localhost:3000
复制代码
必须推荐给基友的Curl工具使用指南

发送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工具使用指南

同时请求结束后,可以看到上传的图片已经被保存下来了:

必须推荐给基友的Curl工具使用指南

curl下载文件

下载保存文件是加-o 保存地址参数。

# 将baidu的html文件下载到本地
curl -o ./my-download.html https://www.baidu.com
复制代码

curl命令执行的效果如下图,而且文件也已经被下载了下来:

必须推荐给基友的Curl工具使用指南

查看响应头参数

-i参数可以返回响应头信息

curl -i  https://www.baidu.com
复制代码
必须推荐给基友的Curl工具使用指南

查看完整的报文信息

curl -v https://www.baidu.com
复制代码
必须推荐给基友的Curl工具使用指南

curl --trace ./log.txt https://www.baidu.com
复制代码
必须推荐给基友的Curl工具使用指南

指定请求的user-agent

我们启动一个最简单的http服务,并且配置好vscodedebug用于我们查看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为:

必须推荐给基友的Curl工具使用指南

chrome浏览器的user-agentMozilla/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
复制代码
必须推荐给基友的Curl工具使用指南

指定请求的跳转

--referer 跳转前url 跳转后url

curl --referer http://localhost:3000 http://localhost:3000/newpath
复制代码

此时发送的请求的req.url是新的地址http://localhost:3000/newpath,并且headers中携带了referer字段。

必须推荐给基友的Curl工具使用指南

请求时携带cookie参数

curl --cookie "k=1&k2=2" http://localhost:3000

# --cookie简写为-b
curl -b "k=1&k2=2" http://localhost:3000
复制代码
必须推荐给基友的Curl工具使用指南

为了使用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工具使用指南

curl -b ./cookie http://localhost:3000
复制代码

此时debug可以看到req上已经携带了我们的cookie:

必须推荐给基友的Curl工具使用指南

结束语

curl对于想随手快速验证一个服务是非常便利的,而且真是越用越好用。希望小伙伴看完这篇教程之后能够使用起来,帮助到大家平日的开发工作。


原文地址:https://juejin.cn/post/7081685548271665183

展开阅读全文

页面更新:2024-04-30

标签:报文   简写   使用指南   参数   上传   代码   地址   文件   工具   数据   图片   信息

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top