[英]Python request to retrieve all requests from Postman collection
我需要打印来自特定 Postman 集合的所有请求。 我有这段代码:
import requests
# Set up Postman API endpoint and authorization
postman_api_endpoint = "https://api.getpostman.com/collections"
postman_api_key = "PMAK-63b6bf724ebf902ad13d4bf2-e683c12d426716552861acda**********"
headers = {"X-Api-Key": postman_api_key}
# Get all requests from Postman collection
collection_id = "25184041-c1537769-f598-4c0e-b8ae-8cd185a79c03"
response = requests.get(f"{postman_api_endpoint}/{collection_id}/items", headers)
if response.status_code != 200:
print("Error retrieving collection:", response.text)
else:
# Print all requests
requests_data = response.json()["items"]
for request_data in requests_data:
request_method = request_data["request"]["method"]
request_url = request_data["request"]["url"]
request_headers = request_data["request"]["header"]
request_body = request_data["request"]["body"]["raw"] \
if request_data["request"]["body"]["mode"] == "raw" else ""
print(f"{request_method} {request_url}")
print("Headers:")
for header in request_headers:
print(f"{header['key']}: {header['value']}")
print("Body:")
print(request_body)
我在尝试调用 response.text 并进行此类按摩时收到错误消息:
检索集合时出错:{"error":{"name":"notFound","message":"Requested resource not found"}}
这意味着我有 404 错误。 我有几个假设我做错了什么:
当然我的集合中有请求,所以错误不可能是因为集合是空的
请给我一些建议,我该如何解决这个错误。 谢谢!
答案其实很简单。 我不知道我需要在 Postman 中按下按钮保存请求。我想如果我在集合中创建请求它会自动保存它。 但是我没有,所以我只是手动保存了所有请求并最终收到了正确的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.