Skip to content

Instantly share code, notes, and snippets.

@antonioshadji
Created August 29, 2024 13:04
Show Gist options
  • Save antonioshadji/8dc1a5e3817f32e8b5bf4d7183b87b82 to your computer and use it in GitHub Desktop.
Save antonioshadji/8dc1a5e3817f32e8b5bf4d7183b87b82 to your computer and use it in GitHub Desktop.
Example of loading library from CDN in postman.
// The example at https://blog.postman.com/adding-external-libraries-in-postman/
// was failing for me due to exports, for this particular library stripping the last 3 lines loaded the library successfully.
// NOTE: this particular library does not work in Postman due to missing dependencies from node/browser
function removeLastLines(text, num) {
// Split the text into lines
const lines = text.split('\n');
// Remove the last two lines
lines.splice(-num);
// Join the remaining lines back into a single string
const modifiedText = lines.join('\n');
return modifiedText;
}
pm.sendRequest("https://unpkg.com/@noble/ed25519", (err, res) => {
//convert the response to text and save it as an environment variable
if (err) {
console.log(err)
return
}
if (res.code === 200) {
var stream = removeLastLines(res.stream.toString(), 3)
}
pm.collectionVariables.set("library", stream);
// eval will evaluate the JavaScript code and initialize the min.js
eval(pm.collectionVariables.get("library"));
// you can call methods in the cdn file using this keyword
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment