Skip to content

Support parsing YAML in postman tests #12557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
spkane opened this issue Dec 21, 2023 · 4 comments
Closed
1 task done

Support parsing YAML in postman tests #12557

spkane opened this issue Dec 21, 2023 · 4 comments

Comments

@spkane
Copy link

spkane commented Dec 21, 2023

Is there an existing request for this feature?

  • I have searched the existing issues for this feature request and I know that duplicates will be closed

Is your feature request related to a problem?

I am making an API calls that returns YAML and I need to be able to parse the YAML, either directly or by converting it into JSON.

Describe the solution you'd like

Ideally, I want to be able to do something like this:

const creds = YAML.parse(kubeconfig)

but even having access to js-yaml would be very useful.

Describe alternatives you've considered

Treating the YAML like a big string and just trying to cut stuff out of it.

Additional context

YAML is used a ton in Kubernetes and other modern platforms, and being able to parse it is important in various circumstances.

@spkane spkane added the feature label Dec 21, 2023
@spkane
Copy link
Author

spkane commented Dec 21, 2023

cc/ @manidharanupoju24

@DannyDainton
Copy link
Contributor

As a potential short term solution you could bring in the lib through a CDN and store the function as a Global variable.

Add this to the Pre-request script (At the level that makes sense to you):

if (!pm.globals.has('jsyaml_lib')) {
    pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js", (err, res) => {
        pm.globals.set("jsyaml_lib", res.text());
    })
}

I've used globals here so that it's open to anything in the Workspace, It's also only going to make that async call once to store the variable.

As a usage example, you can use the libs functions in your tests like this:

// Import yaml-js library (stored in global variable)
const yaml = pm.globals.get('jsyaml_lib');
(new Function(yaml))();

let yamlFile = `
- martin:
    name: Martin D'vloper
    job: Developer
    skills:
      - python
      - perl
      - pascal
- tabitha:
    name: Tabitha Bitumen
    job: Developer
    skills:
      - lisp
      - fortran
      - erlang
`
// Convert data to JSON which was initially in YAML format
let specs = jsyaml.load(yamlFile);
console.log(specs);

I've just mocked out a .yaml response to demo purposes but you can replace that with jsyaml.load(pm.response.text());

That will output something like this:

image

@spkane
Copy link
Author

spkane commented Jan 2, 2024

@DannyDainton Thanks. That is a helpful workaround and general pattern.

However, I still believe that this should be integrated directly into Postman since working with YAML data is very common nowadays, and being able to at least convert between the types would be incredibly useful.

That being said, this is a very useful workaround for the time being.

@appurva21
Copy link
Member

Hey @spkane! You can now directly import js-yaml (or any package from NPM) into your scripts without any workarounds.

const jsyaml = pm.require('npm:js-yaml');

For more details on how to use external packages in scripts, check out: #13542

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants