bruno icon indicating copy to clipboard operation
bruno copied to clipboard

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

Open Nerkho opened this issue 2 years ago • 4 comments

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

The issue is similar to https://github.com/usebruno/bruno/pull/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"
}

Nerkho avatar Feb 21 '24 09:02 Nerkho

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);

Its-treason avatar Feb 22 '24 21:02 Its-treason

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

Nerkho avatar Feb 28 '24 16:02 Nerkho

Any updates on this bug?

q587p avatar Mar 11 '24 14:03 q587p

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.

dw-0 avatar Mar 19 '24 11:03 dw-0