From 49b0f3a3223ad899c84a1e01cf983e7e3400eb29 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Thu, 27 Apr 2023 19:25:52 +0530 Subject: [PATCH] fix(#148) : fixed issue where form url encoded params were not being interpolated --- packages/bruno-cli/src/runner/interpolate-vars.js | 9 +++++++++ packages/bruno-cli/src/runner/prepare-request.js | 3 +-- packages/bruno-cli/src/runner/run-single-request.js | 6 ++++++ packages/bruno-electron/src/ipc/network/index.js | 6 ++++++ .../bruno-electron/src/ipc/network/interpolate-vars.js | 9 +++++++++ .../bruno-electron/src/ipc/network/prepare-request.js | 3 +-- 6 files changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/bruno-cli/src/runner/interpolate-vars.js b/packages/bruno-cli/src/runner/interpolate-vars.js index 6debaffb0..cf7109ad2 100644 --- a/packages/bruno-cli/src/runner/interpolate-vars.js +++ b/packages/bruno-cli/src/runner/interpolate-vars.js @@ -42,6 +42,15 @@ const interpolateVars = (request, envVars = {}, collectionVariables ={}) => { request.data = interpolate(request.data); } } + } else if(request.headers["content-type"] === "application/x-www-form-urlencoded") { + if(typeof request.data === "object") { + try { + let parsed = JSON.stringify(request.data); + parsed = interpolate(parsed); + request.data = JSON.parse(parsed); + } catch (err) { + } + } } else { request.data = interpolate(request.data); } diff --git a/packages/bruno-cli/src/runner/prepare-request.js b/packages/bruno-cli/src/runner/prepare-request.js index bcc388d9c..44d3465b8 100644 --- a/packages/bruno-cli/src/runner/prepare-request.js +++ b/packages/bruno-cli/src/runner/prepare-request.js @@ -1,5 +1,4 @@ const { get, each, filter } = require('lodash'); -const qs = require('qs'); const prepareRequest = (request) => { const headers = {}; @@ -41,7 +40,7 @@ const prepareRequest = (request) => { const params = {}; const enabledParams = filter(request.body.formUrlEncoded, (p) => p.enabled); each(enabledParams, (p) => (params[p.name] = p.value)); - axiosRequest.data = qs.stringify(params); + axiosRequest.data = params; } if (request.body.mode === 'multipartForm') { diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index 659536107..e2ce6a839 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -1,3 +1,4 @@ +const qs = require('qs'); const chalk = require('chalk'); const { forOwn, each, extend, get } = require('lodash'); const FormData = require('form-data'); @@ -39,6 +40,11 @@ const runSingleRequest = async function (filename, bruJson, collectionPath, coll // interpolate variables inside request interpolateVars(request, envVariables, collectionVariables); + // stringify the request url encoded params + if(request.headers['content-type'] = 'application/x-www-form-urlencoded') { + request.data = qs.stringify(request.data); + } + // run request const response = await axios(request); diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 1c9b0ace4..734af95f5 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -1,3 +1,4 @@ +const qs = require('qs'); const axios = require('axios'); const Mustache = require('mustache'); const FormData = require('form-data'); @@ -124,6 +125,11 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { interpolateVars(request, envVars, collectionVariables); + // stringify the request url encoded params + if(request.headers['content-type'] = 'application/x-www-form-urlencoded') { + request.data = qs.stringify(request.data); + } + // todo: // i have no clue why electron can't send the request object // without safeParseJSON(safeStringifyJSON(request.data)) diff --git a/packages/bruno-electron/src/ipc/network/interpolate-vars.js b/packages/bruno-electron/src/ipc/network/interpolate-vars.js index c9845f230..8e54430c1 100644 --- a/packages/bruno-electron/src/ipc/network/interpolate-vars.js +++ b/packages/bruno-electron/src/ipc/network/interpolate-vars.js @@ -42,6 +42,15 @@ const interpolateVars = (request, envVars = {}, collectionVariables ={}) => { request.data = interpolate(request.data); } } + } else if(request.headers["content-type"] === "application/x-www-form-urlencoded") { + if(typeof request.data === "object") { + try { + let parsed = JSON.stringify(request.data); + parsed = interpolate(parsed); + request.data = JSON.parse(parsed); + } catch (err) { + } + } } else { request.data = interpolate(request.data); } diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index ef2feb45e..9305c25ff 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -1,5 +1,4 @@ const { get, each, filter } = require('lodash'); -const qs = require('qs'); const prepareRequest = (request) => { const headers = {}; @@ -39,7 +38,7 @@ const prepareRequest = (request) => { const params = {}; const enabledParams = filter(request.body.formUrlEncoded, (p) => p.enabled); each(enabledParams, (p) => (params[p.name] = p.value)); - axiosRequest.data = qs.stringify(params); + axiosRequest.data = params; } if (request.body.mode === 'multipartForm') {