概述
httpbin.org 是一个简单、免费、开源的 HTTP 请求/响应测试服务。前端工程师调试接口、后端工程师验证请求、DevOps 测试网络——只要一行 curl,就能得到一个”原样返回”的响应。无需注册、即开即用。
相关说明
- 官方站点:https://httpbin.org
- 开源地址:https://github.com/postmanlabs/httpbin
- Docker 镜像:
kennethreitz/httpbin- 编程语言:Python + Flask
[toc]
1. 是什么?解决什么问题?
httpbin.org 会把发过去的 HTTP 请求原样返回。
你发什么,它返回什么——包括请求头、请求体、查询参数、Cookie、认证信息等。适合用来:
- 验证 HTTP 客户端的请求是否按预期构造
- 测试边界情况:超大 Body、特殊 Header、延迟响应
- 检查代理/Gateway 是否正确转发请求
- 演示/调试 REST API 不用起本地服务
用官方文档的话说:“A simple HTTP Request & Response Service.”
2. 快速开始
2.1 在线使用(无需安装)
直接在浏览器或命令行访问:
# 最基础的 GET 测试
curl https://httpbin.org/get
# POST 请求
curl -X POST https://httpbin.org/post
# 带查询参数
curl "https://httpbin.org/get?token=abc123&user=alice"2.2 Docker 私有化部署
不想用公网服务?想在内网测试?
# 一行命令启动
docker run -p 80:80 kennethreitz/httpbin
# 服务地址变为
# http://localhost:80docker-compose.yml(生产级配置参考):
version: "3.8"
services:
httpbin:
image: kennethreitz/httpbin:latest
container_name: httpbin
ports:
- "8080:80"
restart: unless-stopped
environment:
- FLASK_APP=httpbin:app
networks:
- test-net
networks:
test-net:
driver: bridge启动:
docker compose up -d
# 访问 http://localhost:8080提示:私有部署后,所有请求都不会经过公网,适合测试内网应用、CI/CD 流水线隔离环境。
3. 核心端点详解
下文所有端点均以
https://httpbin.org为基准。私有用本地部署则替换为http://localhost:8080。
3.1 基本请求方法
| 端点 | 方法 | 说明 |
|---|---|---|
/get | GET | 原样返回所有查询参数和请求头 |
/post | POST | 原样返回 POST body、表单、JSON |
/put | PUT | 原样返回 PUT body |
/patch | PATCH | 原样返回 PATCH body |
/delete | DELETE | 测试 DELETE 请求 |
示例 — POST 发送 JSON:
curl -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "role": "developer"}'返回:
{
"args": {},
"data": "{\"name\": \"Alice\", \"role\": \"developer\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "36",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/8.0.0"
},
"json": {
"name": "Alice",
"role": "developer"
},
"origin": "203.0.113.42",
"url": "https://httpbin.org/post"
}3.2 状态码与延迟
| 端点 | 说明 |
|---|---|
/status/418 | 返回 HTTP 418 I’m a teapot(趣味彩蛋) |
/status/500 | 返回任意 HTTP 状态码(替换 418 为目标码) |
/delay/3 | 延迟 N 秒返回(最大 10 秒) |
示例 — 测试超时处理:
# 模拟 3 秒延迟
curl -w "\nTime: %{time_total}s\n" \
https://httpbin.org/delay/3用途:测试客户端超时配置是否合理,验证重试逻辑。
3.3 请求头与身份信息
| 端点 | 说明 |
|---|---|
/headers | 返回所有请求头 |
/ip | 返回请求来源 IP 地址 |
/user-agent | 返回 User-Agent 头 |
/anything | 返回请求的完整信息(包含 URL、Method、Headers) |
示例 — 验证代理转发:
curl https://httpbin.org/ip
# 返回:{"origin": "203.0.113.42"}
curl -H "X-Forwarded-For: 10.0.0.1" \
-H "X-Real-IP: 10.0.0.1" \
https://httpbin.org/headers
# 查看代理头是否被正确转发3.4 响应格式
| 端点 | 说明 |
|---|---|
/json | 返回 JSON 格式示例数据 |
/html | 返回简单的 HTML 页面 |
/robots.txt | 返回 robots.txt 内容 |
/xml | 返回 XML 格式数据 |
/base64/SGVsbG8gV29ybGQ= | 解码 Base64 字符串 |
3.5 压缩与编码
| 端点 | 说明 |
|---|---|
/gzip | 返回 Gzip 压缩过的响应(验证 gzip 解压) |
/deflate | 返回 Deflate 压缩响应 |
/brotli | 返回 Brotli 压缩响应 |
示例 — 验证客户端解压能力:
curl -H "Accept-Encoding: gzip" \
https://httpbin.org/gzip | \
gunzip3.6 重定向与状态
| 端点 | 说明 |
|---|---|
/redirect/2 | 跳转到指定次数(临时重定向) |
/absolute-redirect/2 | 绝对路径重定向 |
/relative-redirect/2 | 相对路径重定向 |
示例 — 跟踪重定向链:
curl -L -v https://httpbin.org/redirect/2
# -L 跟随重定向,-v 显示详细信息3.7 流式响应
| 端点 | 说明 |
|---|---|
/stream/10 | 流式返回 N 行 JSON |
/links/5 | 返回包含 N 个链接的 HTML |
3.8 图片与媒体
| 端点 | 说明 |
|---|---|
/image | 返回 JPEG 图片 |
/image/png | 返回 PNG 图片 |
/image/webp | 返回 WebP 图片 |
/image/svg | 返回 SVG 图片 |
示例 — 下载测试图片:
curl -o test.png https://httpbin.org/image/png3.9 Cookies
| 端点 | 说明 |
|---|---|
/cookies | 返回所有发送的 Cookie |
/cookies/set?name=Alice&role=dev | 设置 Cookie 并重定向 |
/response-headers?name=Alice | 在响应头中返回自定义值 |
3.10 缓存控制
| 端点 | 说明 |
|---|---|
/cache | 返回缓存相关头(验证 ETag、Last-Modified) |
/etag/:etag | 测试 ETag 一致性验证 |
4. 常用场景示例
4.1 调试 API 请求构造
前端发请求给后端,但参数总是不对?用 httpbin 逐字段排查:
curl -X POST https://httpbin.org/post \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..." \
-H "Content-Type: application/json" \
-d '{
"page": 1,
"pageSize": 20,
"filters": {
"status": "active",
"tags": ["vip", "verified"]
}
}'返回中 json 字段就是后端收到的数据,一目了然。
4.2 测试超时配置
用 /delay 模拟网络慢:
# 测试 8 秒超时是否生效
curl -m 5 https://httpbin.org/delay/8
# -- 超时报错:curl: (28) Operation timed out4.3 验证认证 Token 传递
curl https://httpbin.org/headers \
-H "Authorization: Bearer YOUR_TOKEN_HERE"
# 在返回的 headers 中确认 Authorization 是否正确传递4.4 测试 HTTP 方法覆盖
for method in GET POST PUT PATCH DELETE; do
echo "=== $method ==="
curl -X $method https://httpbin.org/$method \
-s | jq -r '.url'
done4.5 CI/CD 流水线健康检查
# GitHub Actions 示例
- name: Test HTTP client
run: |
response=$(curl -s -o /dev/null -w "%{http_code}" \
https://httpbin.org/status/200)
if [ "$response" -ne 200 ]; then
echo "HTTP test failed"
exit 1
fi5. 私有部署最佳实践
5.1 固定端口映射
生产环境建议固定容器端口,方便团队约定:
docker run -d \
--name httpbin \
-p 8888:80 \
-p 8443:443 \
--restart=always \
kennethreitz/httpbin5.2 配合 nginx 做 HTTPS 终结
server {
listen 443 ssl;
server_name httpbin.your-company.local;
ssl_certificate /etc/ssl/certs/httpbin.crt;
ssl_certificate_key /etc/ssl/private/httpbin.key;
location / {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}5.3 日志查看
# 查看容器日志
docker logs -f httpbin
# 实时查看请求日志
docker logs httpbin --tail 20 -f6. 替代方案
| 工具 | 特点 | 适合场景 |
|---|---|---|
| httpbin.org | 简单、免费、开源 | 快速验证、最小依赖 |
| Postman Echo | 图形化界面、团队协作 | API 开发调试 |
| Mocky.io | 自定义响应、免费 | 前端模拟后端 |
| WireMock | 本地部署、功能强大 | 复杂模拟场景 |
| Jsonplaceholder | 仅返回 JSON | 前端占位数据 |
如果只需要”原样返回”,httpbin 是最轻量的选择。如果需要自定义响应逻辑,WireMock 更合适。
7. 参考链接
- 官方站点:https://httpbin.org
- GitHub:https://github.com/postmanlabs/httpbin
- Docker Hub:https://hub.docker.com/r/kennethreitz/httpbin
🖼️ 配图 Prompt(供 AI 生成)
封面图(主视觉)
用途:文章封面、社交媒体配图
A clean technical illustration of HTTP API testing workflow.
A laptop screen showing colorful JSON request/response data flowing
back and forth, with a glowing API endpoint symbol at the center.
Dark navy blue background, neon cyan and purple accents,
modern developer aesthetic. Minimal, professional, no text.
场景图(技术示意)
用途:内文插图,展示 curl 命令执行效果
A dark terminal window showing colorful HTTP request/response
in a modern CLI. Multiple terminal tabs visible with JSON data,
code syntax highlighting, and a browser window showing httpbin
response in the background. Dark theme, vibrant accent colors.