How to use the command 'bru' (with examples)

How to use the command 'bru' (with examples)

The ‘bru’ command forms the command-line interface for Bruno, an open-source Integrated Development Environment (IDE) specifically designed for exploring and testing APIs. This tool is particularly useful for developers and QA engineers who frequently work with API requests and need an efficient way to automate and manage them. By offering various functionalities through the command line, ‘bru’ allows for seamless integration into automated scripts and workflows, optimizing the process of API testing and exploration.

Run all request files from the current directory

Code:

bru run

Motivation:

Running all request files in the current directory is a valuable feature when managing multiple API requests. Often, during API development or testing, there is a need to execute several requests sequentially or in batch. This command simplifies that task by running every request file present in your current working directory, thereby saving time and reducing manual efforts.

Explanation:

  • bru: This is the command-line utility for Bruno, the open-source IDE.
  • run: This sub-command instructs ‘bru’ to execute the actions defined in the request files.

Example Output:

Executing: GetUser.bru...
GET Request to https://api.example.com/users
Response: 200 OK
...
Executing: CreatePost.bru...
POST Request to https://api.example.com/posts
Response: 201 Created
...
All requests executed successfully.

Run a single request from the current directory by specifying its filename

Code:

bru run file.bru

Motivation:

In scenarios where you need to isolate the execution of a particular request, perhaps for debugging or focused testing, this command is indispensable. By allowing the execution of a single request file, it offers precision and control over the testing process.

Explanation:

  • bru: The command-line utility for Bruno.
  • run: Executes the API request.
  • file.bru: This specifies the particular request file you wish to execute. Replace file.bru with the actual filename.

Example Output:

Executing: CustomRequest.bru...
POST Request to https://api.example.com/custom
Response: 200 OK
Request completed successfully.

Run requests using an environment

Code:

bru run --env environment_name

Motivation:

API requests may need to vary based on the environment (development, testing, production) they point to. Using environments allows developers to dynamically change variables like API endpoints, credentials, or other configuration settings without altering the request files themselves.

Explanation:

  • bru: The Bruno CLI.
  • run: Triggers the API requests.
  • --env environment_name: This flag specifies which environment configuration to use, allowing you to access different variables and endpoints.

Example Output:

Environment: Production
Executing: UserFetch.bru...
GET Request to https://api.production.example.com/users
Response: 200 OK
Request executed in production environment.

Run requests using an environment with a variable

Code:

bru run --env environment_name --env-var variable_name=variable_value

Motivation:

Customizing requests further by injecting environment variables at runtime is extremely powerful for testing and conditional execution. It allows temporary overrides for session tokens, API keys, or other sensitive data that might need dynamic insertion based on testing criteria.

Explanation:

  • bru: Commands the Bruno interface.
  • run: Instigates the request instructions.
  • --env environment_name: Denotes the set environment.
  • --env-var variable_name=variable_value: Injects a specific environmental variable for the session, allowing you to temporarily change critical details.

Example Output:

Environment: Staging
Setting variable: apiKey=12345XYZ
Executing: DataRetrieval.bru...
Authorization: Bearer 12345XYZ
GET Request to https://api.staging.example.com/data
Response: 200 OK
Variable injection successful.

Run requests and collect the results in an output file

Code:

bru run --output path/to/output.json

Motivation:

Capturing the results of API requests in an output file provides a way to log responses for later analysis, auditing, or debugging. This feature is crucial for running batch processes where results need to be reviewed, archived, or compared over time.

Explanation:

  • bru: Invokes the Bruno command-line tool.
  • run: Commences the request operations.
  • --output path/to/output.json: Directs the output responses to be saved into a specified file location, useful for documentation or further examination.

Example Output:

Executing: CatalogFetch.bru...
Output will be saved to path/to/output.json
GET Request to https://api.example.com/catalog
Response saved: 200 OK
Request results successfully recorded.

Display help

Code:

bru run --help

Motivation:

Accessing the help section is indispensable for new users or when troubleshooting unexpected behaviors or errors. This command displays a comprehensive guide to available sub-commands, their usages, and descriptions within the ‘bru’ command-line tool.

Explanation:

  • bru: Calls the Bruno interface.
  • run: Specific sub-command to get detailed help on.
  • --help: Displays the help document for the run sub-command, detailing arguments, options, and usage.

Example Output:

Usage: bru run [options] [file]
Options:
  --env <environment>     Specify environment to use
  --env-var <name=value>  Set an environment variable
  --output <file>         Specify output file for responses
  --help                  Output usage information

Conclusion:

The ‘bru’ command-line tool exemplifies a robust solution for developers and testers who require streamlined operations when handling API requests. Whether executing requests in bulk, targeting specific environments, or recording outputs, the versatility and precision of ‘bru’ allow for enhanced productivity and testing accuracy, making it an invaluable asset in the toolkit of API developers and testers.

Related Posts

How to Use the Command 'dockutil' (with Examples)

How to Use the Command 'dockutil' (with Examples)

Dockutil is a versatile command-line utility designed to manage items in the macOS dock.

Read More
How to use the command 'kustomize' (with examples)

How to use the command 'kustomize' (with examples)

Kustomize is a command-line tool designed to streamline the deployment of Kubernetes resources.

Read More
Exploring the 'wami' Command for Program Recommendations (with examples)

Exploring the 'wami' Command for Program Recommendations (with examples)

The ‘wami’ command-line tool is a powerful and easy-to-use utility that helps users find appropriate software programs and tools for specific tasks by searching across various sources.

Read More