Master Bruno’s powerful scripting capabilities with JavaScript
Bruno provides a powerful JavaScript scripting engine that allows you to automate complex workflows, manipulate requests and responses, and integrate with external libraries. Scripts can be executed at different stages of the request lifecycle.
Pre-request scripts run before Bruno sends the HTTP request. Use them to set headers, modify the request body, generate dynamic values, or perform authentication.
// Create multipart form data programmaticallyconst FormData = require("form-data");const form = new FormData();form.append('user_id', bru.getVar('userId'));form.append('file_name', 'document.pdf');form.append('timestamp', new Date().toISOString());req.setBody(form);
Post-response scripts execute after receiving the response. Use them to extract data, set variables for subsequent requests, or perform custom processing.
// Parse response and set variablesconst data = res.getBody();// Set runtime variablesbru.setVar('userId', data.user.id);bru.setVar('sessionToken', data.session.token);// Set environment variablesbru.setEnvVar('authToken', data.auth.token);// Persist variables across collectionsbru.setEnvVar('refreshToken', data.refresh_token, { persist: true });
req.getUrl() // Get full URLreq.setUrl(url) // Set URLreq.getHost() // Get hostnamereq.getPath() // Get pathreq.getQueryString() // Get query string
Header Methods
req.getHeaders() // Get all headersreq.getHeader(name) // Get specific headerreq.setHeader(name, val) // Set headerreq.setHeaders(obj) // Set multiple headersreq.deleteHeader(name) // Delete headerreq.deleteHeaders(arr) // Delete multiple headers
Body Methods
req.getBody() // Get parsed bodyreq.getBody({ raw: true })// Get raw body stringreq.setBody(data) // Set body (auto-stringifies JSON)req.setBody(data, { raw: true }) // Set raw body
Other Methods
req.getMethod() // Get HTTP methodreq.setMethod(method) // Set HTTP methodreq.getName() // Get request namereq.getPathParams() // Get path parametersreq.getTags() // Get request tagsreq.setTimeout(ms) // Set timeoutreq.setMaxRedirects(num) // Set max redirects
res.getStatus() // Get status coderes.getStatusText() // Get status textres.getResponseTime() // Get response time in ms
Header Methods
res.getHeaders() // Get all headersres.getHeader(name) // Get specific header
Body Methods
res.getBody() // Get parsed response bodyres.setBody(data) // Modify response bodyres.getDataBuffer() // Get raw bufferres.getSize() // Get size { header, body, total }
Direct Access
// Shorthand property accessres.status // Same as res.getStatus()res.headers // Same as res.getHeaders()res.body // Same as res.getBody()// Query response data directlyres('user.email') // Get nested propertyres('items[0].name') // Array access