Skip to main content

Lib that generates postman collections out of requests

Project description

request2postman


Small lib that allows you to automatically generate postman collections out of python requests lib

Using requests hooks

from request2postman import Collection, request_to_postman_hook
from requests import Session


collection = Collection("some_name")
with Session() as session:
    session.hooks["response"].append(request_to_postman_hook(collection))
    session.get("https://httpbin.org/basic-auth/user/pass")
    session.post("https://httpbin.org/basic-auth/user/pass", json={"key": "value"})

with open("collection.json", "w") as file:
    file.write(collection.json)

Using Request object directly

from request2postman import Collection
import requests


resp1 = requests.get("https://httpbin.org/basic-auth/user/pass")
resp2 = requests.post("https://httpbin.org/basic-auth/user/pass", json={"key": "value"})

collection = Collection("some_name")
collection.add_request(resp1.request, name="request1")
collection.add_request(resp2.request)

with open("collection.json", "w") as file:
    file.write(collection.json)

Result

{
  "info": {
    "_postman_id": "6fde81f7-6cb8-4cde-bf89-a2b476ed794f",
    "name": "some_name",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "event": [],
  "variable": [],
  "item": [
    {
      "id": "79731ba3-25e5-4149-a975-20c4e7929728",
      "name": "request1",
      "response": [],
      "event": [],
      "request": {
        "url": {
          "raw": "https://httpbin.org/basic-auth/user/pass",
          "protocol": "https",
          "path": [
            "basic-auth",
            "user",
            "pass"
          ],
          "host": [
            "httpbin",
            "org"
          ],
          "query": [],
          "variable": []
        },
        "header": [],
        "method": "GET"
      }
    },
    {
      "id": "0a75dc4e-16ef-468e-a903-412832fd5749",
      "name": "/basic-auth/user/pass",
      "response": [],
      "event": [],
      "request": {
        "url": {
          "raw": "https://httpbin.org/basic-auth/user/pass",
          "protocol": "https",
          "path": [
            "basic-auth",
            "user",
            "pass"
          ],
          "host": [
            "httpbin",
            "org"
          ],
          "query": [],
          "variable": []
        },
        "header": [
          {
            "key": "Content-Length",
            "value": "16"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "method": "POST",
        "body": {
          "mode": "raw",
          "raw": "{\"key\": \"value\"}"
        }
      }
    }
  ]
}

You can skip some headers to avoid posting Authorization headers or any other private headers with others

from request2postman import Collection
import requests


collection = Collection("some_name", skip_headers=["Authorization"])
resp1 = requests.get("https://httpbin.org/basic-auth/user/pass")
resp2 = requests.post("https://httpbin.org/basic-auth/user/pass", json={"key": "value"})
collection.add_request(resp1.request)
collection.add_request(resp2.request)

with open("collection.json", "w") as file:
    file.write(collection.json)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

request2postman-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file request2postman-0.1.1.tar.gz.

File metadata

  • Download URL: request2postman-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.0

File hashes

Hashes for request2postman-0.1.1.tar.gz
Algorithm Hash digest
SHA256 eecb99a23fbefd9ffae11ca19be4a4f4f40a01a0f98bea339755b321a03de799
MD5 35f551b464f9c927afa874fe28ddb632
BLAKE2b-256 cde1f8a74c30f3364f576be0f2c2326add0143ac6db0e6d0468fefffe5371ae5

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page