Writing script with postman node js libaries

Hi,

I would like to create script which take regular postman collection (json file)
and will convert it to be more generic , using variables, pre-request script and tests.
means, lets say my first response in collection contains student name in body/headers , and the next request contains this student name in request body/headers, I want the converted collection to have:1. for first request in tests script , script which will save this student name as a variable 2. for second request to have that variable saved on 1 on the request body/header as variable.

is any postman lib which support such an action?
any idea/direction how to do that?

thanks!

Hi @TehilaRubitstein, welcome to the community!

There’s no postman library that does this. Best thing that I can say to get you close to what you want is to use Open API Spec with custom extensions to define which variables come from which endpoints, then use the Postman API to load the spec and process accordingly.

What you’re asking for is actually a huge project, but it would be awesome if you could get it done.

use Open API Spec with custom extensions to define which variables come from which endpoints - what is the meaning here? also why need to define which variables come from which endpoint? why not to search for each value in request , same value in previous responses and after finding such - to define a variable with this value which will be used from this request further without searching…

what stoping you from using pm.environment.set(“variable”,“value”) in test script session of firrst request and in second request body just refer that by {{variable}}

my goal is to have these test script and those {{variables}}, but since I have lots of hard coded collections I dont want to write it manually , and looking for way to do it Auto. by script that will convert those collections to have test scripts and pre-request scripts etc

Could you add an example , I am not able to understand what you mean by hard coded collection

If you download collection as json then you can change the script in this part and upload again . iF thats what you are trying to do

lets say this is the source collection :
…“item”: [
{
“name”: “first request in collection”,
“request”: {
“method”: “POST”,
“header”: [
{
“key”: “Content-Type”,
“value”: “application/json”
}
],
“body”: {
“mode”: “raw”,
“raw”: {…}
},
“url”: {

},
“description”: “calling first request”
},
"response": [
…"{“newPet”:{“petName”:somia,“PetHobby”:game}}"…
]
},
{
“name”: “second request in collection”,
"request": {
“method”: “POST”,
“header”: [
{
“key”: “Content-Type”,
“value”: “application/json”
}
],
“body”: {
“mode”: “raw”,
"raw": "{“newPet”:{“petName”:somia,“PetHobby”:game}}"
},
“url”: {

},
“description”: “calling first request”
},
“response”:
}
]

this is the desired result: (its not the accurate format I know but only for idea explaination)

“item”
: [
{
“name”: “first request in collection”,
"event":[
{
listen:test
script:{
…pm.collectionVariables.set(newPet,data.newPet…);…
}
}
]
“request”: {
“method”: “POST”,
“header”: [
{
“key”: “Content-Type”,
“value”: “application/json”
}
],
“body”: {
“mode”: “raw”,
“raw”: {…}
},
“url”: {

},
“description”: “calling first request”
},
“response”: [
…"{“newPet”:{“petName”:somia,“PetHobby”:game}}"…
]
},
{
“name”: “second request in collection”,
“request”: {
“method”: “POST”,
“header”: [
{
“key”: “Content-Type”,
“value”: “application/json”
}
],
“body”: {
“mode”: “raw”,
“raw”: …{{newPet}}
},
“url”: {

},
“description”: “calling first request”
},
“response”:
}
]

I want to write a script which will be run on such source collections ( big amount of collections…) and will convert them to be with tests script part etc Automaticly

can someone reply if postman has any node js libary which support such activity? beside of postman-collection?
I can see inside postman-collection there is variableList.replace() but I need for my script just the opposite functionality - any one aware of?
while : variableList.replace("{{variableName}}/blabla/bla") => variableValue/blabla/bla
I need function which will do the opposite:
will get variableValue/blabla/bla and will retrive "{{variableName}}/blabla/bla"

There is not. You will have to write this yourself.

ok - thnx 4 info

1 more question - I can see this libary on the npm: grunt-postman-variables in its documentation mentioned : Replace Postman variables in files from .postman_globals and/or .postman_environment file

do u think this libary might help in writing this script?

lets say script will first collect the duplicated values from the collection into file then will call this libary to replace all of them to params etc

Are you trying to do this natively within Postman?

Or is this some script you’re doing outside of it? While you can use external npm packages in the Postman sandbox, it isn’t exactly an easy thing to do.

the script I am writing is outside of postman,
this script will run on some postman .json collections without env variables /collection variables and want the script to create “generic” collections based on them means to make the collection with collection variables and pre-request scripts to enable feature use …see above example