【问题标题】:Parsing postman collection with python to create requests用python解析邮递员集合以创建请求
【发布时间】:2018-03-20 10:17:51
【问题描述】:

我有一个邮递员集合,其中包含多个文件夹,其中包含大约 100 个请求,包括 GET、POST、DELETE、PUT 等所有方法。

我需要在 python 中解析 postman.json 集合文件并创建请求并写入 txt 文件。 这些请求需要传递给另一个工具。你能帮我吗。任何指针都会有所帮助。 我被困在解析集合 JSON 文件中,这非常困难。

【问题讨论】:

  • 你试过json.loads()吗??然后作为字典访问

标签: python json python-requests postman


【解决方案1】:

如何指导

阅读有关JSON encoder/decoder的文档

解析一个 JSON 字符串 json_str

import json

obj = json.loads(json_str)

解析 JSON 文件:

import json
import io

with io.open("path/to/file.json", mode-"rb") as fd:
    obj = json.load(fd)

阅读有关Requests的文档

有一个requests.request()函数,你可以在其中传递方法(GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD)参数。用法:

import requests

response = requests.request('GET', 'http://httpbin.org/get')

写入binary file

您可以将请求的结果写入二进制文件。您也可以使用文本文件,但某些响应可能是二进制的,例如,如果您下载图像。

with io.open("path/to/file.dat", mode="wb") as fd:
    fd.write(response.content)

【讨论】:

    【解决方案2】:

    下面的代码将帮助您了解如何解析集合 JSON 文件,以便您可以递归地累积该集合中的所有请求。我使用了Postman Collection SDK 以及一个名为Lodash 的辅助实用程序。

    您可以使用下面的这个 sn-p 来获取您希望通过 Python 请求使用的请求信息。或者为了简单起见,尽可能使用 Javascript。

    var fs = require('fs'), // needed to read JSON file from disk
      sdk = require('postman-collection'),
      Collection = sdk.Collection,
      Request = sdk.Request,
      Item = sdk.Item,
      ItemGroup = sdk.ItemGroup,
      _ = require('lodash'),
      myCollection,
      requests = [],
      dfs = function (item, requests) { // fn -> Depth first search
        // Check if this is a request
        if (Item.isItem(item)) {
          if (item.request && Request.isRequest(item.request)) {
            requests.push(item.request);
          }
        }
        // Check if this is a nested folder
        else if (ItemGroup.isItemGroup(item)) {
          // Check if this is an empty folder
          if (item.items && (item.items.count() === 0)) {
            return requests;
          }
          // Do a depth first search for requests on the nested folder
          item.each(function (item) {
            requests.push(dfs(item, []));
          })
        }
    
        return requests;
      };
    
    // Load a collection to memory from a JSON file on disk 
    myCollection = new Collection(JSON.parse(
      fs.readFileSync('<path_to_your_collection_json_file>').toString()));
    
    myCollection.items.each(function (item) {
      // Check if this is a request at the top level
      if (Item.isItem(item)) {
        if (item.request && Request.isRequest(item.request)) {
          requests.push(item.request);
        }
      }
      // Check if this is a folder at the top level
      else if (ItemGroup.isItemGroup(item)) {
        item.items.each(function (item) {
          requests.push(dfs(item, []));
        })
      }
    });
    
    // Flatten. After flattening requests will an array of PostmanRequest objects
    requests = _.flattenDeep(requests)
    
    // Serialize each PostmanRequest to it's JSON representation
    requests = _.map(requests, (r) => { return r.toJSON(); })
    
    _.each(requests, (request) => {
      console.log(request.url); // The request URL
      console.log(request.method); // The HTTP Verb of your request Eg. GET, POST
      _.each(request.header, (header) => {
        console.log(header.key, header.value); // Eg. key -> 'Content-Type', value -> 'application/json'
      });
      // You can also access the request body and the auth, certificate and proxy used by the request
      // Your PostmanRequest description is also available
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 2023-03-29
      • 1970-01-01
      • 2019-10-12
      • 2017-06-17
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      相关资源
      最近更新 更多