PropertyList is not defined in pre-request script

Hi folks, I’m trying to emulate the behavior of Ruby’s encode_www_form in a pre-release script. (If there’s a quick way to do this, I’d appreciate that, but otherwise, this is the issue I’m currently facing:)

pm.request.body.urlencoded is a PropertyList. It even says so in Postman when I hover over it.

When I try any methods on it (e.g. map) it says that PropertyList is not defined. Do I need to import it somehow? Here is a public workspace showing the issue:

I’ve tried it on the web and also on the OS X native app (Apple silicon).

Hey @ataul :wave:

Welcome to the Postman Community! :postman:

I’m not sure what your use case is or what you’re trying to achieve but you’d need to use this at the top of the script, to make that work:

PropertyList = require('postman-collection').PropertyList

The postman-collection is an external module that’s part of the scripts sandbox:

1 Like

Thanks Danny, this works! Is the dot notation after the require something that’s Postman-specific or is this a common language feature?

At the moment, I’m trying to URI encode the keys/values from a form body and join them back up with &. When I used encodeURI or encodeURIComponent, it also transformed the & which wasn’t what I needed. Accessing the property list means I can map the entries into the format I need:

// for key: "password", value: "at_next_@_and_next_&"

PropertyList = require('postman-collection').PropertyList

function encode(entry) {
    return `${encodeURIComponent(entry.key)}=${encodeURIComponent(entry.value)}`

// "password=at_next_@_and_next_%26"
// "password=at_next_%40_and_next_%26"

It’s a common language feature and it allows you to access a function inside the object. I’m not even going to pretend like I know this topic inside out, I really don’t but I know you can do that. :grimacing:

In terms of the Postman Sandbox, if you remove the .PropertyList and then added a . again, you’d be shown the other functions.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.