![]() Importing the Day.js library by sending a request to the CDN and saving it as a collection variable Load a library from a variable The eval function is a powerful and helpful tool, but since it just evaluates JavaScript code represented as a string, it can be a security risk to run random code with the eval function. Security note: Make sure you feel comfortable with the library you’re importing this way. you can call methods in the cdn file using this keyword eval will evaluate the JavaScript code and initialize the min.jsĮval(pm.collectionVariables.get("dayjs_library")) convert the response to text and save it as an environment variable After running the eval function, the methods for the desired library are attached to the this keyword and can be used in the sandbox.With the code saved as a string, use the eval function to evaluate the JavaScript code.Once you receive a response, convert the response to a string and save it as a collection variable.If a CDN version of a library exists, one option is to make a request to the CDN page hosting the library in the pre-request or tests tabs. For these examples, we’ll import Day.js, a JavaScript library to help parse and format dates, and see how to use some of the Day.js methods. If a CDN version of a library exists, or you have the pure JavaScript code for that library, we’ll look at two methods of importing a library. Use built-in libraries, like lodash, by importing them with the require statement Importing additional external librariesįor libraries that are not included in the Postman sandbox, you may still be able to make use of their functionality within Postman. To see the full list of available libraries with links to the documentation for each, check out the Postman Learning Center. Here’s a basic example of using the format method from the moment library to format a date: let moment = require('moment') Ĭonsole.log("formatted date =", moment().format("MMM Do YYYY")) To do this, use the require method by passing the module name as a parameter and assigning the return object from the method to a variable. ![]() Some popular JavaScript and NodeJS modules (such as moment, lodash, and querystring) are included in the Postman sandbox and can be imported by calling the require method. ![]() If you’re a hands-on learner and want to see working examples, fork the Adding External Libraries collection to see the various methods of adding external libraries. Related: Use the Using Libraries Template We’ll first look at how to use the libraries that are available out of the box, then review some methods for adding your own desired libraries. However, you also have the ability to leverage additional external libraries. There are several helpful libraries built into the sandbox whose methods and functionality are available with very little extra work needed. This is where external libraries come in. While these objects help a ton with testing your API and workflow control, you may need additional functionality. ![]() The sandbox has built-in JavaScript APIs via the pm and postman objects for testing your request and response data. The Postman sandbox is an environment provided to execute Javascript code in the pre-request and tests scripts for a request.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |