curl、python-requests、postman和jmeter的对应关系

发布于:2025-09-01 ⋅ 阅读:(22) ⋅ 点赞:(0)

一、初识curl

curl 是一个功能强大的命令行工具,用于传输数据,支持多种协议(如 HTTP、HTTPS、FTP 等)。

分析以下curl:

curl "https://$HOST/mon/adm/au/opera" --header "Authorization: $AUTH" -X POST -H 'Content-Type: application/json' --data '{"reportFormUid":"01", "operationType":"mute", "action": "ON"}'

这是一个典型的 ‌HTTPS POST 请求‌,用于向服务器发送 JSON 格式的操作指令。

1. 请求基础信息

  • URL‌:https://$HOST/mon/adm/au/opera

    • $HOST 是变量,需替换为实际主机地址(如 api.example.com)。
    • 路径 /mon/adm/au/opera 可能指向管理后台的某个操作接口。
  • 方法‌:POST
    明确指定为 POST 请求,通常用于提交数据。

2. 请求头(Headers)

  • Authorization: $AUTH
    • 用于Authorization(授权)身份验证,$AUTH 需替换为有效的凭证(如 Bearer token 或 Basic base64编码)。
  • Content-Type: application/json
    声明请求体为 JSON 格式。

3. 请求体(Body)

  • JSON 数据‌:
    {"reportFormUid":"01", "operationType":"mute", "action": "ON"}
    
  • reportFormUid: "01":可能标识操作目标的表单或资源。
  • operationType: "mute":操作类型为“静音”。
  • action: "ON":执行开启静音的操作。

4.一些细节

同样是发送 JSON 数据:

1、curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com
2、curl "https://$HOST/mon/adm/au/opera" --header "Authorization: $AUTH" -X POST -H 'Content-Type: application/json' --data '{"reportFormUid":"01", "operationType":"mute", "action": "ON"}'

5. 文件操作

  • 下载文件‌:
    curl -O https://example.com/file.zip

    -O 使用服务器原始文件名保存。

  • 断点续传‌:
    curl -C - -O https://example.com/largefile.zip

    -C - 自动恢复未完成的下载。

  • 上传文件‌:
    curl -F "file=@localfile.txt" https://example.com/upload

    -F 用于表单文件上传。

6. 调试与高级功能

  • 显示请求详情‌:
    curl -v https://example.com

    -v 输出请求和响应的头部信息。

  • 跟随重定向‌:
    curl -L https://example.com/short-url

    -L 自动跳转。

  • 使用代理‌:
    curl -x http://proxy.example.com:8080 https://target.com

    -x 指定代理服务器。

  • 仅获取响应头‌:
    curl -I https://example.com

    -I 只显示 HTTP 头部。

  • 监控网站状态‌(状态码):
    curl -s -o /dev/null -w "%{http_code}" https://example.com

    返回 HTTP 状态码。

7. 认证与安全

  • Basic 认证‌:
    curl -u username:password https://secure.com

    -u 传递用户名和密码。

  • 忽略 SSL 证书验证‌:
    curl -k https://self-signed.example.com

    -k 跳过证书检查(不推荐生产环境使用)。

二、curl信息转化为python发送请求

import requests


url = "https://HOST/mon/adm/au/opera"
headers = {
    "Authorization": "AUTH_TOKEN",
    "Content-Type": "application/json"
}
data = {
    "reportFormUid": "01",
    "operationType": "mute",
    "action": "ON"
}

response = requests.post(url, headers=headers, json=data)
#response = requests.request(url, headers=headers, json=data)
print(response.status_code, response.text)

三、curl在postman中使用

1、import

2、替换变量


网站公告

今日签到

点亮在社区的每一天
去签到