Linux怎么发送HTTP请求 Linux使用curl命令测试接口【技巧】
技术百科
穿越時空
发布时间:2026-01-27
浏览: 次 curl是Linux下测试HTTP接口的核心工具,支持GET、POST、认证、自定义Header、超时控制及文件上传等全场景用法。
如果您需要在Linux系统中向远程服务器发送HTTP请求以测试接口功能,curl命令是最常用且功能强大的工具之一。以下是多种使用curl发送不同类型HTTP请求的具体方法:
一、发送GET请求
GET请求用于从服务器获取资源,是测试接口最基本的方式。curl默认即为GET方法,无需额外指定。
1、在终端中输入curl后跟目标URL,例如:curl https://httpbin.org/get。
2、如需显示响应头信息,添加 -I 参数:curl -I https://httpbin.org/get。
3、若需保存响应内容到文件,使用 -o 参数:curl -o response.json https://httpbin.org/get。
二、发送POST请求并携带JSON数据
POST请求常用于向服务器提交结构化数据,如API调用中传递JSON格式的请求体。
1、使用 -X POST 明确指定方法,并通过 -H 设置Content-Type头:curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' https://httpbin.org/post。
2、若JSON内容较长,可将数据写入文件后用 @ 引用:curl -X POST -H "Content-Type: application/json" -d @data.json https://httpbin.org/post。
3、添加 -v 参数可查看完整请求与响应过程:curl -v -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://httpbin.org/post。
三、发送带认证的HTTP请求
当目标接口需要身份验证时,curl支持

1、使用Basic认证时,提供用户名和密码:curl -u "username:password" https://httpbin.org/basic-auth/user/pass。
2、使用Bearer Token认证时,在Authorization头中传入Token:curl -H "Authorization: Bearer abc123xyz" https://httpbin.org/bearer。
3、若需同时发送Cookie,使用 -b 参数:curl -b "sessionid=abc; token=xyz" https://httpbin.org/cookies。
四、发送带自定义Header和超时控制的请求
某些接口要求特定请求头或对响应时间敏感,可通过参数精确控制请求行为。
1、添加任意自定义Header,使用多次 -H 参数:curl -H "X-API-Key: 12345" -H "Accept: application/json" https://httpbin.org/headers。
2、设置连接超时时间为5秒,使用 --connect-timeout 5:curl --connect-timeout 5 https://httpbin.org/delay/3。
3、设置总请求超时为10秒,使用 --max-time 10:curl --max-time 10 https://httpbin.org/delay/8。
五、上传文件至服务器
当接口支持文件上传(如multipart/form-data格式),curl可通过 -F 参数模拟表单提交。
1、上传本地文件file.txt,字段名为“upload”:curl -F "upload=@file.txt" https://httpbin.org/post。
2、同时上传多个文件并附带文本字段:curl -F "file1=@a.jpg" -F "file2=@b.png" -F "description=test" https://httpbin.org/post。
3、指定文件MIME类型,避免服务端解析错误:curl -F "document=@report.pdf;type=application/pdf" https://httpbin.org/post。
# app
# 工具
# word
# linux
# linux系统
# js
# json
# curl
# session
# pdf
# cookie
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- Win11怎么关闭触摸键盘图标_Windows11
- Go语言中slice追加操作的底层共享机制解析
- Win10如何卸载微软拼音输入法 Win10只保留
- Win11怎样安装搜狗输入法_Win11安装搜狗输
- Win11怎么关闭防火墙通知_屏蔽Win11安全中
- php本地部署后数据库连接报错_1045acces
- Python文件操作优化_大文件与流处理解析【教程
- C++中的协变与逆变是什么?C++函数指针与返回类
- Win11怎么关闭用户账户控制UAC_Window
- Win11怎么关闭触控板_Win11笔记本禁用触摸
- Win11怎么清理C盘系统日志_Win11清理系统
- C++如何使用std::optional?(处理可
- 如何在Golang中捕获HTTP服务器错误_Gol
- Go 语言标准库为何不提供泛型 Contains
- Linux如何安装JDK11_Linux环境变量配
- TestNG的testng.xml配置文件怎么写
- Win11如何设置开机问候语 Win11修改登录界
- Win11文件扩展名怎么显示_Win11查看文件后
- Windows怎样关闭Edge新标签页广告_Win
- Windows10电脑怎么设置自动连接WiFi_W
- LINUX的SELinux是什么_详解LINUX强
- Windows 10怎么录屏_Windows 10
- Win10怎么设置开机密码_Windows10账户
- 如何诊断并终止卡死的 multiprocessin
- Win11怎么查看已连接wifi密码 Win11查
- Win11怎么设置夜间模式_Windows11显示
- Win11如何暂停系统更新 Win11暂停更新最长
- c++ std::atomic如何保证原子性 c+
- Win11相机打不开提示错误怎么修_相机权限开启与
- php能控制zigbee模块吗_php通过串口与c
- Win11怎么更改盘符_Win11磁盘管理修改驱动
- Python数据挖掘核心算法实践_聚类分类与特征工
- php嵌入式日志记录怎么实现_php将硬件数据写入
- Mac怎么给文件夹加密_Mac创建加密磁盘映像教程
- 如何在 PHP 单元测试中正确模拟带方法的图像处理
- Python路径拼接规范_跨平台处理说明【指导】
- php做exe支持多线程吗_并发处理实现方式【详解
- 如何使用Golang处理网络超时错误_Golang
- php嵌入式需要什么环境_搭建php+linux嵌
- MAC如何安装Git版本控制工具_MAC开发环境配
- Win11怎么关闭自动调节亮度 Win11禁用内容
- Win11怎么查看显卡显存_查询Win11显卡详细
- 如何在Golang中处理URL参数_Golang
- Win11关机快捷键是什么_Win11快速关机方法
- 如何在Golang中实现文件下载_Golang文件
- Win11如何更新显卡驱动 Win11检查和安装设
- 如何在 Go 开发中正确处理本地包导入与远程模块路
- Win10系统映像怎么恢复 Win10使用系统映像
- Python多线程使用规范_线程安全解析【教程】
- php485函数执行慢怎么优化_php485性能提

QQ客服