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);
}
2
Answers
In the modified snippet, the line
let data = res.getBody().toString().trim();
now includes thetoString()
andtrim()
methods. ThetoString()
method converts the raw response data into a string format, while thetrim()
method removes any leading or trailing whitespace from the string. These changes ensure the bearer token is properly processed and cleaned before being stored in the bearer_token environment variable.Your token is generally a key of your data object (and not the "data" itself), in the "Post Response" field, the following script should work :