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

Env var not evaluated correctly in pre-request for v18 v19 v20 #2557

Open
2 tasks done
james-yin opened this issue Jul 3, 2024 · 2 comments · May be fixed by #4122
Open
2 tasks done

Env var not evaluated correctly in pre-request for v18 v19 v20 #2557

james-yin opened this issue Jul 3, 2024 · 2 comments · May be fixed by #4122
Assignees
Labels

Comments

@james-yin
Copy link

I have checked the following:

  • I use the newest version of bruno.
  • I've searched existing issues and found nothing related to my issue.

Describe the bug

Since v18, the environment variable is not getting evaluated correctly in pre-request. As you can see in my screenshots, in v17, the env var gets evaluated with the correct value while v20 does not get evaluated at all and stays with the double brackets {{}}. I'm also attaching my simple test file/collection.

image

image

.bru file to reproduce the bug

Test collection to reproduce the above bug reported.

bruno-test.zip

Screenshots/Live demo link

image
image

@helloanoop
Copy link
Contributor

The behavior of the request object (URLs, headers, body) being interpolated before the pre-request execution has caused several issues.

For example, consider the following scenario:

The request URL is:
http://localhost:8080/users?token={token}

And the pre-request script contains:

bru.setVar("token", "secret token");

In this case, if interpolation happens before the pre-request script is executed, the code in the script becomes ineffective, as the URL would already have been interpolated without the updated token value.

That said, I agree that there should be a way to enable request object interpolation ahead of time when needed. I’m considering introducing a bru.interpolate() API that can be invoked at the start of the script to address this requirement.

@TanguyP
Copy link

TanguyP commented Feb 14, 2025

I find this to be an annoying limitation on an otherwise fantastic piece of software.

The above example seems contrived to me: why would you use a var in that case? I believe there are some cleaner options which don't require any assumptions about variable interpolation not having been performed yet.

So in order to make this contrived example work, Bruno is breaking my (IMO perfectly normal) use case for which I need interpolation to have been performed before my script is run:

  • I'm using an API which requires me to sign the request. In my pre-request script, I'm calculating the signature which is based on URL+headers+body.
  • My request uses an environment variable for the API's base URL, so the request's URL looks like {{base_url}}/some/path.
  • Therefore, when I'm calculating the signature, I have to perform the variable interpolation on the URL myself.

As a workaround, I've implemented this function which returns the interpolated URL (using environment variables only, which is what I need, but I guess Bruno also allows other types of variables in the URL, so this function could be improved):

function getInterpolatedUrl() {
	const url = req.getUrl();
	const variableRegex = /\{\{([^\}]+)\}\}/g;
	return url.matchAll(variableRegex).reduce((transformedUrl, match) => transformedUrl.replaceAll("{{" + match[1] + "}}", bru.getEnvVar(match[1])), url);
}

@sanish-bruno sanish-bruno linked a pull request Feb 27, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants