You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Postman API导入Postman集合的技术问题咨询

通过Postman API导入集合的请求体Schema及Shell脚本示例

核心请求体Schema(遵循Postman Collection v2.1标准)

创建集合的请求体必须符合Postman官方的Collection v2.1结构,核心必填及常用字段如下:

  • info: 集合基础信息,必填,包含name(集合名称)、schema(固定值为https://schema.getpostman.com/json/collection/v2.1.0/collection.json)
  • item: 集合内的请求/文件夹数组,可选(创建空集合时可传入空数组)

最小可用请求体示例:

{
  "info": {
    "name": "我的测试集合",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": []
}

带示例请求的完整请求体:

{
  "info": {
    "name": "用户管理集合",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "获取用户列表",
      "request": {
        "method": "GET",
        "url": "https://api.example.com/users",
        "header": [
          {
            "key": "Authorization",
            "value": "Bearer {{authToken}}"
          }
        ]
      }
    },
    {
      "name": "创建用户",
      "request": {
        "method": "POST",
        "url": "https://api.example.com/users",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\"username\": \"test_user\", \"email\": \"test@example.com\"}"
        }
      }
    }
  ]
}

Shell脚本实现示例(使用curl)

你可以用curl发送POST请求完成集合创建,需配置Postman API密钥和目标工作区ID:

#!/bin/bash

# 配置参数
API_KEY="你的Postman API密钥"
WORKSPACE_ID="目标工作区的ID"
COLLECTION_FILE="./local-collection.postman_collection.json"

# 读取本地集合文件内容
COLLECTION_CONTENT=$(cat "$COLLECTION_FILE")

# 发送创建请求
curl -X POST "https://api.getpostman.com/collections" \
  -H "X-Api-Key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"collection\": $COLLECTION_CONTENT, \"workspace\": \"$WORKSPACE_ID\"}"

关键注意事项

  • X-Api-Key: 从Postman的「设置」>「API密钥」页面生成并获取
  • WORKSPACE_ID: 在目标工作区的「设置」>「工作区ID」中查看
  • 若无需导入本地文件,可直接在脚本中构造COLLECTION_CONTENT的JSON内容,替代文件读取步骤

内容的提问来源于stack exchange,提问作者Charith Hettiarachchi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 16:01:05