Skip to content
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

Support files as values to variables #32

Open
neilmatthewlott opened this issue Jul 21, 2023 · 3 comments
Open

Support files as values to variables #32

neilmatthewlott opened this issue Jul 21, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@neilmatthewlott
Copy link

Is your feature request related to a problem?

No

Describe the solution you'd like

I'd like to be able to specify a value as a file location that automatically resolves and the content of the file gets put into the variable. E.g. jwt below would have the value of file:///Users/nl/work/data/jwt. This would facility not having to paste the JWT each time.

{
staging: {
baseUrl: "https://taskmanagement.cognosos.dev/api/v0",
jwt: "file:///Users/nl/work/data/jwt"
},
}

Additional context

No response

@neilmatthewlott neilmatthewlott added the enhancement New feature or request label Jul 21, 2023
@AntonShuvaev
Copy link
Collaborator

Thanks for the suggestion. I will consider adding this in the future.

@AntonShuvaev
Copy link
Collaborator

Currently, there is no built-in function for reading files, but there is a workaround. Since the script runs on GraalVM JS, you can use Java classes. To read a file and assign its content to a variable, you can write the following in your pre-request script:

const Files = Java.type('java.nio.file.Files');
const Paths = Java.type('java.nio.file.Paths');

const path = Paths.get("path_to_your_jwt");

try {
    jc.variables.set("jwt", Files.readString(path));
} catch (e) {
    console.error("Error reading file: " + e.message);
}

You can then use the jwt variable wherever you need it.

@AntonShuvaev
Copy link
Collaborator

Added the jc.readFile function to read file contents. You can now set a variable's value to the contents of a file using this script: jc.variables.set("jwt", jc.readFile('path')).

In the future, I plan to implement inline functions for variable values, like this: jwt: {{readFile('path')}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants