postman-collection
postman-collection copied to clipboard
Query parameters included on JSON.stringify even when omitted
Currently, when I construct a Url
without any query parameters, the instance of the url
works as expected in isolation. Both toString()
and toJSON()
leave out any query string.
However, when I add the same url to a collection, the query
value is filled with a lot of extra fields that persist when imported back into Postman.
The code:
var fs = require("fs"),
{
Collection,
Item,
ItemGroup,
Request,
Url
} = require("postman-collection");
var url = new Url("https://www.example.com");
var item = new Item({
name: "Test",
request: new Request({
url
}),
});
var itemGroup = new ItemGroup();
itemGroup.items.add(item);
var collection = new Collection({
item: itemGroup
});
console.log("Collection Stringify: ", JSON.stringify(collection, null, 2));
outputs:
Collection Stringify: {
"item": [
{
"id": "9f670340-f4c4-4379-8fbc-be33d445c20d",
"item": [
{
"id": "57e49c56-9f7e-471a-b23e-f9f5fbf243c0",
"name": "Test",
"request": {
"url": {
"protocol": "https",
"host": [
"www",
"example",
"com"
],
"query": [
{
"key": "members",
"value": [
{
"key": "members",
"value": []
},
{
"key": "reference",
"value": {}
},
{
"key": "Type",
"value": {
"_postman_propertyName": "QueryParam",
"_postman_propertyIndexKey": "key",
"_postman_propertyAllowsMultipleValues": true
}
},
{
"key": "_postman_listIndexKey",
"value": "key"
},
{
"key": "_postman_listAllowsMultipleValues",
"value": true
}
]
},
{
"key": "reference",
"value": {
"members": {
"key": "members",
"value": []
},
"reference": {
"key": "reference",
"value": {}
},
"Type": {
"key": "Type",
"value": {
"_postman_propertyName": "QueryParam",
"_postman_propertyIndexKey": "key",
"_postman_propertyAllowsMultipleValues": true
}
},
"_postman_listIndexKey": {
"key": "_postman_listIndexKey",
"value": "key"
},
"_postman_listAllowsMultipleValues": {
"key": "_postman_listAllowsMultipleValues",
"value": true
}
}
},
{
"key": "Type",
"value": {
"_postman_propertyName": "QueryParam",
"_postman_propertyIndexKey": "key",
"_postman_propertyAllowsMultipleValues": true
}
},
{
"key": "_postman_listIndexKey",
"value": "key"
},
{
"key": "_postman_listAllowsMultipleValues",
"value": true
}
],
"variable": []
},
"method": "GET"
},
"response": [],
"event": []
}
],
"event": []
}
],
"event": [],
"variable": [],
"info": {
"_postman_id": "5ad37d6a-5bef-4cba-9821-036b34b287c8",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
}
}
If I'm doing something incorrectly, would someone be able to point me in the right direction?
I have ran into this same issue and even if I add query parameters this is all I get. The same occurs for urlencoded body.
@tklun @jbelford you are directly trying to stringify the complete Collection object by doing
console.log("Collection Stringify: ", JSON.stringify(collection, null, 2));
The query parameters will be correctly parsed if you first convert the collection object to JSON Object(by doing collection.toJSON()) and then stringify. The right way of stringifying the collection object is as follows:
console.log("Collection Stringify: ", JSON.stringify(collection.toJSON()));
Did you really test it? I still can reproduce it in both examples.
I found it out. You really need to call toJSON
on all objects Request
, Collection
, ...
Invalid
collectionDefinition.item?.push({
name: op.Name,
request: new Request(request),
});
valid
collectionDefinition.item?.push({
name: op.Name,
request: new Request(request).toJSON(),
});
Hi Everybody! @StarpTech I tried your solution but I still get the postman_listIndex polluted query params
Couldn't get it to work either :/ at this point im just writing my own types