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

[BUG] [>1.6.1] Array variables created with pre-request scripts are not treated as variables in Body. #1635

Open
Nerkho opened this issue Feb 21, 2024 · 4 comments

Comments

@Nerkho
Copy link

Nerkho commented Feb 21, 2024

I recently noticed an issue that happens with version 1.7.0 and onwards.

The issue is similar to #1217, but also affects the JSON request body and as far as I noticed only with variables created by a pre-request script.
The issue doesn't appear with version 1.6.1 and lower.

In the pre-request script, we create an array to be used in the request body.

const project = bru.getEnvVar("PROJECT");
const roles = (bru.getEnvVar("ROLES").split(",")).map(element => {
  return element.trim();
});

var project_roles = [];

for (let i in roles) {
  project_roles.push("\""+project+"_"+roles[i]+"\"");
}

bru.setVar("roles", project_roles);

Request body :

{
  "foo" : {{roles}}
}

In the response, we see the that the variable was treated as a plain value.

{
  "args": {},
  "data": "\"{\\n  \\\"foo\\\" : {{roles}}\\n}\"",
  "files": {},
  "form": {},
  "headers": {
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "postman-echo.com",
    "x-amzn-trace-id": "Root=1-65d5c392-33b6d41c1866a3384c94b2c7",
    "content-length": "29",
    "accept": "application/json, text/plain, */*",
    "content-type": "application/json",
    "request-start-time": "1708508050633",
    "user-agent": "axios/1.6.7",
    "accept-encoding": "gzip, compress, deflate, br"
  },
  "json": null,
  "url": "https://postman-echo.com/post"
}

This only seems to impact Arrays.
If I replace my pre-request script with a simple string...

bru.setVar("roles", "bar");

It works fine :

{
  "args": {},
  "data": "\"{\\n  \\\"foo\\\" : bar\\n}\"",
  "files": {},
  "form": {},
  "headers": {
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "postman-echo.com",
    "x-amzn-trace-id": "Root=1-65d5c439-78924a0e68d4e1394df59f22",
    "content-length": "23",
    "accept": "application/json, text/plain, */*",
    "content-type": "application/json",
    "request-start-time": "1708508217412",
    "user-agent": "axios/1.6.7",
    "accept-encoding": "gzip, compress, deflate, br"
  },
  "json": null,
  "url": "https://postman-echo.com/post"
}
@Its-treason
Copy link
Member

Its-treason commented Feb 22, 2024

You can, you use a workaround like this, in the pre-request script. This will send the array correctly.

const body = req.getBody();

body.test = ["Hello", "World"];

req.setBody(body);

@Nerkho
Copy link
Author

Nerkho commented Feb 28, 2024

Thanks that seemed to work !
Is that the correct way going forward or just a temporary workaround ?

@q587p
Copy link

q587p commented Mar 11, 2024

Any updates on this bug?

@dw-0
Copy link
Contributor

dw-0 commented Mar 19, 2024

It seems like this is not necessarily a bug with array variables only. I see this issue with regular string variables as well.
In a pre-request script i have the following code:

const moment = require("moment");
const now = moment();
const formatNow = now.format('yyyy-MM-DD HH:mm:ss.SSS')
console.log(now);
bru.setVar("now", now);
bru.setVar("formatNow", formatNow);

In my request body i use now and formatNow like usual with {{now}} and {{formatNow}}.
Upon sending the request, those variables are send as plain text, and not the actual values. Although the values are correctly set in the collection variables:
image

Im kind of amazed that this issue doesn't get more attention.

Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 24, 2024
Interpolate will now fallback to the original object to collect
variable values. Objets will automaticly json encoded. And if
intererpolate is executed for the json body all variables will
be json encoded so strings get double quoted.

This should fix:
- usebruno#1910
- usebruno#1635
Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 25, 2024
Interpolate will now fallback to the original object to collect
variable values. Objets will automaticly json encoded. And if
intererpolate is executed for the json body all variables will
be json encoded so strings get double quoted.

This should fix:
- usebruno#1910
- usebruno#1635
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

5 participants