[Feature] Run a request via script
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');
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.
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.
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 : 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 Would you be able to share a video of the glitch ?
@helloanoop : Here is a video of a colllection that uses bru.setNextRequest() to jump back from the third to the first request.
Two issues:
- The UI appends new items to the list of requests that have been run, but updates the state of the first instances
- There is no way to stop the infinite loop from the UI.
https://github.com/usebruno/bruno/assets/20513065/26a37aaa-89ae-4267-acd4-63cc7f8d4a34
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:
The UI appends new items to the list of requests that have been run, but updates the state of the first instances
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?
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
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")
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
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.
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)
agree with @thiagomini
Error is being thrown when run from cli.
Error is something like this
folder\request (bru.runRequest is not a function)
Hi, we're working on the implementing these functions in the CLI. Will post updates here!
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
Hi @ArturoBlazquez , we're working on the autocomplete. Will post updates here about the release.
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?
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!
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 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 👍
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!