usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
26.84k stars 1.23k forks source link

[Feature] Run a request via script #1350

Open mirkogolze opened 10 months ago

mirkogolze commented 10 months ago

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');
mj-h commented 9 months ago

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.

mirkogolze commented 9 months ago

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.

helloanoop commented 9 months ago

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

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

mj-h commented 9 months ago

@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.

helloanoop commented 9 months ago

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

mj-h commented 9 months ago

@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

orached commented 7 months ago

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?

fayeulle commented 3 months ago

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

gowriter commented 2 weeks ago

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")