Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for random data placeholders in request body #3608

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion packages/bruno-app/src/components/RequestTabPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import SecuritySettings from 'components/SecuritySettings';
import FolderSettings from 'components/FolderSettings';
import { getGlobalEnvironmentVariables } from 'utils/collections/index';
import { produce } from 'immer';
import get from 'lodash/get';
import { replacePlaceholders } from 'utils/common/variable-replacer';

const MIN_LEFT_PANE_WIDTH = 300;
const MIN_RIGHT_PANE_WIDTH = 350;
Expand Down Expand Up @@ -163,7 +165,47 @@ const RequestTabPanel = () => {
}

const handleRun = async () => {
dispatch(sendRequest(item, collection.uid)).catch((err) =>
let newItem = JSON.parse(JSON.stringify(item));

// Use lodash get to extract body mode
const bodyMode = newItem.draft
? get(newItem, 'draft.request.body.mode')
: get(newItem, 'request.body.mode');

let bodyContent;
if (bodyMode === 'json') {
bodyContent = newItem.draft ? newItem.draft.request.body.json : newItem.request.body.json;
} else if (bodyMode === 'text') {
bodyContent = newItem.draft ? newItem.draft.request.body.text : newItem.request.body.text;
} else if (bodyMode === 'xml') {
bodyContent = newItem.draft ? newItem.draft.request.body.xml : newItem.request.body.xml;
} else if (bodyMode === 'sparql') {
bodyContent = newItem.draft ? newItem.draft.request.body.sparql : newItem.request.body.sparql;
} else {
dispatch(sendRequest(item, collection.uid)).catch(err => { /* handle error */ });
return;
}

// Perform placeholder replacements
bodyContent = replacePlaceholders(bodyContent);

// Assign updated content back to the item
if (bodyMode === 'json') {
if (newItem.draft) newItem.draft.request.body.json = bodyContent;
else newItem.request.body.json = bodyContent;
} else if (bodyMode === 'text') {
if (newItem.draft) newItem.draft.request.body.text = bodyContent;
else newItem.request.body.text = bodyContent;
} else if (bodyMode === 'xml') {
if (newItem.draft) newItem.draft.request.body.xml = bodyContent;
else newItem.request.body.xml = bodyContent;
} else if (bodyMode === 'sparql') {
if (newItem.draft) newItem.draft.request.body.sparql = bodyContent;
else newItem.request.body.sparql = bodyContent;
}

// Send the modified request
dispatch(sendRequest(newItem, collection.uid)).catch((err) =>
toast.custom((t) => <NetworkError onClose={() => toast.dismiss(t.id)} />, {
duration: 5000
})
Expand Down
Loading