调用Postman API获取集合请求报404错误的解决咨询
解决Postman API 404错误的方案
核心问题:请求路径错误
你使用的/collections/{collection_id}/items并非Postman API的合法端点,这是触发404错误的直接原因。Postman API获取集合详情的正确端点是/collections/{collection_id},集合内的所有请求项会包含在返回结果的collection.item字段中。
具体修正步骤
修改请求URL
将原代码中的请求路径:response = requests.get(f"{postman_api_endpoint}/{collection_id}/items", headers)替换为:
response = requests.get(f"{postman_api_endpoint}/{collection_id}", headers)调整数据解析逻辑
接口返回结构变更后,原有的数据提取语句需要修改:
将requests_data = response.json()["items"]替换为:requests_data = response.json()["collection"]["item"]
修正后的完整代码
import requests # 配置Postman API端点和授权信息 postman_api_endpoint = "https://api.getpostman.com/collections" postman_api_key = "PMAK-63b6bf724ebf902ad13d4bf2-e683c12d426716552861acda**********" headers = {"X-Api-Key": postman_api_key} # 获取指定集合内的所有请求 collection_id = "25184041-c1537769-f598-4c0e-b8ae-8cd185a79c03" response = requests.get(f"{postman_api_endpoint}/{collection_id}", headers) if response.status_code != 200: print("获取集合失败:", response.text) else: # 遍历并打印所有请求详情 requests_data = response.json()["collection"]["item"] for request_data in requests_data: request_method = request_data["request"]["method"] # 处理Postman返回的URL结构(可能是字典或字符串) if isinstance(request_data["request"]["url"], dict): url_info = request_data["request"]["url"] request_url = f"{url_info['protocol']}://{url_info['host'][0]}" if url_info.get('path'): request_url += ''.join(url_info['path']) if url_info.get('query'): query_params = '&'.join([f"{q['key']}={q['value']}" for q in url_info['query']]) request_url += f"?{query_params}" else: request_url = request_data["request"]["url"] request_headers = request_data["request"]["header"] request_body = "" if request_data["request"].get("body") and request_data["request"]["body"]["mode"] == "raw": request_body = request_data["request"]["body"]["raw"] print(f"{request_method} {request_url}") print("请求头:") for header in request_headers: print(f"{header['key']}: {header['value']}") print("请求体:") print(request_body) print("---")
额外排查建议
- 确认API密钥权限:如果集合属于团队,需确保你的API密钥拥有该团队的访问权限,个人密钥默认仅能访问个人集合。
- 再次核对集合ID:直接从Postman客户端的集合设置中复制完整ID,避免手动输入时出现字符遗漏或错误。
- 检查网络环境:如果你的环境有代理或防火墙,确认已允许访问
https://api.getpostman.com。
内容的提问来源于stack exchange,提问作者Oleksandr Ovcharenko
相关产品推荐
相关产品推荐

