Skip to content

curl 工具

一.介绍

curl 是一个命令行工具,用于发送 HTTP 请求。它支持多种协议,包括 HTTP、HTTPS、FTP、FTPS、SFTP 等。在调试API时非常有用。

二.安装(ubuntu)

bash
sudo apt install curl

三.常用参数

  • -X:指定请求方法,如 GET、POST、PUT、DELETE 等。
  • -H:指定请求头,如 Content-Type、Authorization 等。
  • -d:指定请求体,如 JSON、XML 等。
  • -o:指定输出文件,将响应内容保存到文件中。

四.基本使用

  • POST 请求 JSON 数据
bash
curl -X POST -H "Content-Type: application/json" -d '{"name":"John", "age":30}' http://example.com/api
  • POST 请求表单数据
bash
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "name=John&age=30" http://example.com/api
  • POST 提交文件
bash
curl -X POST -F "file=@/path/to/file" http://example.com/api
  • GET 请求
bash
curl -X GET http://example.com/api

五.参考