Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scripting a post response in Bruno as an environment variable

I'm trying to grab data from my response in Bruno and set it to the environment variable called bearer_token. Here's the issue. The response is raw and is just the bearer token. So, I'm not sure what I'm supposed to do to parse the raw response without a body. I have tried to use just res as the data and setting this as the bearer token, but when I check my response, low and behold, the environment variable is still blank. I have attached my Post Response script (the one given by the Bruno documentation), and it doesn't work. Any suggestions would be appreciated.

function onResponse(res) {
let data = res.getBody();
bru.setEnvVar("bearer_token", data);
}
like image 970
TylerBalaskovitz Avatar asked Jun 05 '26 05:06

TylerBalaskovitz


2 Answers

Your token is generally a key of your data object and not the "data" itself. In the "Post Response" field of the Script section, the following script should work:

let data = res.body;
bru.setEnvVar("token", data.access_token);
like image 144
demiton Avatar answered Jun 07 '26 20:06

demiton


This is the answer if it's just a response without any other key-value pairs, but just the bearer token as a response.

   let data =res.body ;
    bru.setEnvVar("token",data);
like image 21
TylerBalaskovitz Avatar answered Jun 07 '26 19:06

TylerBalaskovitz