bruno icon indicating copy to clipboard operation
bruno copied to clipboard

[Feature] Run a request via script

Open mirkogolze opened this issue 1 year ago • 8 comments

I think it was mentioned in the discussions around Bruno. But I create here a feature request (If duplicate please close).

It would be useful to have the ability to start a request via script. E.g. to run a dependent pre-request.

The method in the bru-object should return the result of the assertions and the tests and give access to the response object as accessible in the post response script.

  const result = await bru.runRequest('[folder]/[requestFile].bru');

mirkogolze avatar Jan 09 '24 08:01 mirkogolze

Would bru.setNextRequest(<requestName>) help you? It does not allow you to run a request from inside a request, but it allows you to customize the request chaining. You can make the response (or response-properties) available in the next request via bru.setVar(<varName>, <value>) and bru.getVar(<varName>).

Note: Jumping back to earlier requests / the same request currently confuses the UI, but under the hood (and in the CLI) it runs fine.

mj-h avatar Jan 12 '24 09:01 mj-h

I want to use it in a pre-request-script. It think bru.setNextRequest(...) will not perform the chosen request to run before the current request. Could work as a workaround, but would lead to bad workaround request constellations.

mirkogolze avatar Jan 12 '24 11:01 mirkogolze

const result = await bru.runRequest('[folder]/[requestFile].bru');

There have been multiple asks for this feature. Would love to get a PR for this.

helloanoop avatar Jan 12 '24 11:01 helloanoop

@helloanoop : I'm not sure about the "good first issue" tag -- the feature needs to be implemented in the CLI and in the UI. The UI tends to glitch when a request is being run multiple times.

mj-h avatar Jan 12 '24 12:01 mj-h

@mj-h Would you be able to share a video of the glitch ?

helloanoop avatar Jan 12 '24 13:01 helloanoop

@helloanoop : Here is a video of a colllection that uses bru.setNextRequest() to jump back from the third to the first request.

Two issues:

  1. The UI appends new items to the list of requests that have been run, but updates the state of the first instances
  2. There is no way to stop the infinite loop from the UI.

https://github.com/usebruno/bruno/assets/20513065/26a37aaa-89ae-4267-acd4-63cc7f8d4a34

mj-h avatar Jan 15 '24 21:01 mj-h

Hello,

@helloanoop : Here is a video of a colllection that uses bru.setNextRequest() to jump back from the third to the first request.

Two issues:

  1. The UI appends new items to the list of requests that have been run, but updates the state of the first instances

  2. There is no way to stop the infinite loop from the UI.

Bildschirmaufnahme.2024-01-15.um.22.35.44.mov

Would this glitch be resolved?

orached avatar Mar 21 '24 16:03 orached

Hello,

I'm very interested in this feature. I'd like to execute an "auth_request" within the current collection to populate "token" variables.

Is it possible to create a method within the Collection - Settings - Pre-request Script to achieve that?

const result = await bru.runRequest('auth.bru');

Like this image

fayeulle avatar Jul 31 '24 14:07 fayeulle

you can use any http client library to do the work like axios, take a look here:

const axios = require('axios')


const baseUrl = bru.getEnvVar("baseUrl")
const axiosInstance = axios.create({
  baseURL: baseUrl + "auth/",
  headers: {
  'Content-Type': 'application/x-www-form-urlencoded',
  },
});

const data = {
  "phone": bru.getEnvVar("authPhone"),
  "phonePrefix": "+34",
  "language": "es"
}

axiosInstance.post('requestPhoneValidation', data)
.catch(error => {
console.error('Authentication failed:', error);
});


const loginData = {
  "token": bru.getEnvVar("smsCode"),
  "id": bru.getEnvVar("authPhone"),
  "data": "+34",
  "type": "phone",
}

const response = await axiosInstance.post('login', loginData).catch(error => {
console.error('Authentication failed when login:', error);
});

const accessToken = response.data.tokens.token;
console.log(accessToken)
await bru.setVar("token", accessToken)

console.log("Token variable set")

gowriter avatar Oct 16 '24 11:10 gowriter

Yes @gowriter, but I wrote all logic in a dedicated request.

It would be interesting to not rewrite request with axios and just use .bru files. Authentication is just an example. I may have many actions to execute before each request

fayeulle avatar Dec 05 '24 09:12 fayeulle

Another very important thing is to benefit of the proxy configuration of Bruno. With scripting you do not benefit from the applications configuration and have to handle this manually again.

mirkogolze avatar Dec 05 '24 09:12 mirkogolze

Although bru.runRequest works in the UI version, it still throws a (bru.runRequest is not a function) using the latest CLI version (1.38.3)

thiagomini avatar Jan 17 '25 14:01 thiagomini

agree with @thiagomini Error is being thrown when run from cli. Error is something like this folder\request (bru.runRequest is not a function)

deepakacharya avatar Jan 17 '25 17:01 deepakacharya

Hi, we're working on the implementing these functions in the CLI. Will post updates here!

sreelakshmi-bruno avatar Jan 20 '25 11:01 sreelakshmi-bruno

Hi, don't get autocomplete for this new method on Bruno 1.38.1 However it is working fine on the UI if I type the method manually.

(See that on the screenshot I get hints for runner but not for runRequest Screenshot 2025-01-27 at 15 56 33

ArturoBlazquez avatar Jan 27 '25 15:01 ArturoBlazquez

Hi @ArturoBlazquez , we're working on the autocomplete. Will post updates here about the release.

sreelakshmi-bruno avatar Jan 29 '25 10:01 sreelakshmi-bruno

We've implemented some re-request scripting to run a dependant request using bru.runRequest but it seems like the dependencies cannot cross collection boundaries.

In our case we had collection A and B and request in collection B trying to call request in collection A. Seemingly this is not supported and we've only managed to get around this by using a symlink to link collection A to Collection B/dependencies folder.

Is that the expected functionality? If so, any plans to support running requests from multiple collections?

tomdracz avatar Apr 01 '25 08:04 tomdracz

Hi @tomdracz, this was intentional, assuming similar requests would be within the same collection. That said, I agree it would be a great enhancement to allow referencing requests from other collections. I'll update you on the implementation timeline. Thanks!

sreelakshmi-bruno avatar Apr 01 '25 13:04 sreelakshmi-bruno

const result = await bru.runRequest('[folder]/[requestFile].bru'); not working properly in collection level pre request script but working in request level pre request script.

ahujadevansh avatar Apr 02 '25 07:04 ahujadevansh

@ahujadevansh Yep, I can reproduce this! Since collection scripts run for every request, using bru.runRequest() creates an infinite loop (each request triggers the script again). We'll get this fixed soon 👍

anusree-bruno avatar Apr 07 '25 11:04 anusree-bruno

Hi @ahujadevansh , I've created a separate issue for tracking purpose, see https://github.com/usebruno/bruno/issues/4537.

@tomdracz , due to technical limitations, it is not possible to reference request from another collection. Although we could add the functionality to reference requests from a different folder when executing them via folder run. Would grouping similar requests into folders, executing them via folder run work for your usecase? If so, please add a comment in #4538 describing your usecase, Thanks!

Closing this issue since the bru.runRequest() is now available in the app (with autocomplete suggestion) and the CLI. The follow up issues are being tracked in separate issues as mentioned. Thanks!

sreelakshmi-bruno avatar Apr 21 '25 10:04 sreelakshmi-bruno