bruno icon indicating copy to clipboard operation
bruno copied to clipboard

Postman script translation

Open rreyn-bruno opened this issue 7 months ago • 1 comments

I have checked the following:

  • [x] I have searched existing issues and found nothing related to my issue.

This bug is:

  • [x] making Bruno unusable for me
  • [x] slowing me down but I'm able to continue working
  • [x] annoying
  • [ ] this feature was working in a previous version but is broken in the current release.

Bruno version

2.2.0

Operating System

Sequoia 15.1

Describe the bug

We have the below Postman script in hundreds of requests that does not get translated:

var jsonData = JSON.parse(responseBody)

What works in Bruno is:

const jsonData = res.getBody()

Can we have this translation implemented?

.bru file to reproduce the bug

No response

Screenshots/Live demo link

above

rreyn-bruno avatar May 12 '25 12:05 rreyn-bruno

What does responseBody refer to? Could you share a sample collection?

anusree-bruno avatar May 13 '25 07:05 anusree-bruno

postman has legacy APIs, responseCode, responseTime, responseHeaders, responseBody has been deprecated long time ago. In my research, they introduces new apis to replace them back in 2018-2019, and officially started showing warnings by 2020, It's been almost 5 years. Most of the people would have already migrated over to new pm APIs, How popular is the issue?

Got a sample usage of some legacy scripting apis from postman docs

//Set an environment variable
postman.setEnvironmentVariable("key", "value");

//Set a nested object as an environment variable
const array = [1, 2, 3, 4];
postman.setEnvironmentVariable("array", JSON.stringify(array, null, 2));
const obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
postman.setEnvironmentVariable("obj", JSON.stringify(obj));

//Get an environment variable
postman.getEnvironmentVariable("key");

//Get an environment variable whose value is a stringified object
//(Wrap in a try-catch block if the data is coming from an unknown source)
const array = JSON.parse(postman.getEnvironmentVariable("array"));
const obj = JSON.parse(postman.getEnvironmentVariable("obj"));

//Clear an environment variable
postman.clearEnvironmentVariable("key");

//Set a global variable
postman.setGlobalVariable("key", "value");

//Get a global variable
postman.getGlobalVariable("key");

//Clear a global variable
postman.clearGlobalVariable("key");

//Set which request to run next when using the Collection Runner or Newman
postman.setNextRequest("request_name");

//Check if response body contains a string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");

//Check if response body is equal to a string
tests["Body is correct"] = responseBody === "response_body_string";

//Check for a JSON value
const data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;

//Content-Type is present (Case-insensitive checking)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
//getResponseHeader() method returns the header value, if it exists

//Content-Type is present (Case-sensitive)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

//Response time is less than 200ms
tests["Response time is less than 200ms"] = responseTime < 200;

//Response time is within a specific range
//(lower bound inclusive, upper bound exclusive)
tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001);

//Status code is 200
tests["Status code is 200"] = responseCode.code === 200;

//Code name contains a string
tests["Status code name has string"] = responseCode.name.has("Created");

//Successful POST request status code
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

sanish-bruno avatar Jul 04 '25 19:07 sanish-bruno

In our enterprise, almost all of the collections use this... for us, at least, everyone keeps using and cloning the things that they know and that work in Postman. please...

toehser avatar Jul 07 '25 14:07 toehser

Our code is riddled with stuff like: var jsonData = JSON.parse(responseBody); postman.setEnvironmentVariable("dealId", jsonData.dealId);

toehser avatar Jul 07 '25 14:07 toehser