postmanlabs / postman-collection

Javascript module that allows a developer to work with Postman Collections

Home Page:https://www.postmanlabs.com/postman-collection/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

raw is missing during new Item create in postman collection

pipaliyachirag opened this issue · comments

code:

const fs = require("fs");
const { Collection,Url,Header,Item } = require('postman-collection');


(async ()=>{
  const collection = new Collection({
    info: {
      name:"shadow",
    },
  })
  const rawHeaderString =
    'Authorization:\nContent-Type:application/json\ncache-control:no-cache\n';

// Parsing string to postman compatible format
  const rawHeaders = Header.parse(rawHeaderString);

// Generate headers
  const requestHeader = rawHeaders.map((h) => new Header(h));
// Request body
  const requestPayload = {
    key1: 'value1',
    key2: 'value2',
    key3: 'value3'
  };

  collection.items.add(new Item({
    name:"item1",
    request:{
      url:Url.parse("http://localhost:3000/api/v1/users"),
      method:"post",
      header:requestHeader,
      body: {
        mode: 'raw',
        raw: JSON.stringify(requestPayload),
      },
    }
  }))

  const collectionJSON = collection.toJSON();

  fs.writeFile('./collection.json', JSON.stringify(collectionJSON,null,2), (err) => {
    if (err) { console.log(err); }
    console.log('File saved');
  });
})();

expected output:

{
  "item": [
    {
      "id": "69e1560e-b51f-498f-b3f5-f0b802fe6c0e",
      "name": "item1",
      "request": {
        "url": {
          "raw": "http://localhost:3000/api/v1/users",
          "protocol": "http",
          "host": [
            "localhost"
          ],
          "port": "3000",
          "path": [
            "api",
            "v1",
            "users"
          ]
        },
        "header": [
          {
            "key": "Authorization",
            "value": ""
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "cache-control",
            "value": "no-cache"
          }
        ],
        "method": "POST",
        "body": {
          "mode": "raw",
          "raw": "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}"
        }
      },
      "response": [],
      "event": []
    }
  ],
  "event": [],
  "variable": [],
  "info": {
    "_postman_id": "0cfa6f5f-9b10-40c8-b539-d724b9360c2a",
    "name": "shadow",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  }
}

but getting:

{
  "item": [
    {
      "id": "6cac2a38-a8fe-471c-bf4c-1e22e4908fef",
      "name": "item1",
      "request": {
        "url": {
          "protocol": "http",
          "port": "3000",
          "path": [
            "api",
            "v1",
            "users"
          ],
          "host": [
            "localhost"
          ],
          "query": [],
          "variable": []
        },
        "header": [
          {
            "key": "Authorization",
            "value": ""
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "cache-control",
            "value": "no-cache"
          }
        ],
        "method": "POST",
        "body": {
          "mode": "raw",
          "raw": "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}"
        }
      },
      "response": [],
      "event": []
    }
  ],
  "event": [],
  "variable": [],
  "info": {
    "_postman_id": "141fe14f-9c09-421b-83d1-37e321569bd1",
    "name": "shadow",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  }
}

raw field is missing in url object during export.