Repository: usebruno/bruno

Issue:
New Content-Type "fix" is breaking a GET call with a content-type Header
### I have checked the following:

- [x] I have searched existing issues and found nothing related to my issue.

### This bug is:

- [ ] making Bruno unusable for me
- [ ] slowing me down but I'm able to continue working
- [ ] annoying
- [x] this feature was working in a previous version but is broken in the current release.

[Jira](https://usebruno.atlassian.net/browse/BRU-2021)

### Bruno version

2.13.0

### Operating System

macOS 15.7.1

### Describe the bug

For v2.13.0, there is a bug fix entitled "Don't set Content-Type on requests without a body". Instead, if a request with no body DOES have a Content-Type Header, instead of ignoring it, Bruno throws an error:

Pre-Request Script Error
params is not iterable

### .bru file to reproduce the bug

_No response_

### Screenshots/Live demo link

Request:
▼
 Headers
(1)
> Content-Type:application/x-www-form-urlencoded
▼
 Body
No Body found

Response:
Error 00
▼
 Headers
No Headers found
▼
 Body
No Body found
In Bruno version 2.13.1, the method of req.setBody is used to report an error.
### I have checked the following:

- [x] I have searched existing issues and found nothing related to my issue.

### This bug is:

- [x] making Bruno unusable for me
- [ ] slowing me down but I'm able to continue working
- [ ] annoying
- [ ] this feature was working in a previous version but is broken in the current release.

[Jira](https://usebruno.atlassian.net/browse/BRU-2022)

### Bruno version

2.13.1

### Operating System

Windows 10 19042.870

### Describe the bug

When I upgraded from version 2.12.0 to version 2.13.1, I got an error in using the req.setBody method in the pre-request.

Script
```
const data = {
  user_id: 123
}
req.setBody(data);
```

Response

> Pre-Request Script Error
> params is not iterable

### .bru file to reproduce the bug

_No response_

### Screenshots/Live demo link

https://github.com/user-attachments/assets/282c20e9-f02f-4fbd-909b-c82cd158356e


Discussion:
Thanks for reporting this. We are looking into it right now.

Thanks for reporting . We are looking into it right now.



Patch:
diff --git a/packages/bruno-cli/src/utils/form-data.js b/packages/bruno-cli/src/utils/form-data.js
index 180651c967..5741d794ee 100644
--- a/packages/bruno-cli/src/utils/form-data.js
+++ b/packages/bruno-cli/src/utils/form-data.js
@@ -8,9 +8,14 @@ const path = require('path');
  * @returns {string} Returns a order respecting standard compliant string of form encoded values
  */
 const buildFormUrlEncodedPayload = (params) => {
+  if (typeof params !== 'object') return '';
+  if (!Array.isArray(params)) return '';
   const resultParams = new URLSearchParams();
   for (const param of params) {
-    resultParams.append(param.name, param.value);
+    // Invalid items are ignored
+    if (typeof param !== 'object') continue;
+    if (!('name' in param)) continue;
+    resultParams.append(param.name, param.value ?? '');
   }
   return resultParams.toString();
 };
diff --git a/packages/bruno-electron/src/utils/form-data.js b/packages/bruno-electron/src/utils/form-data.js
index 5e50a76f42..fe53c31cce 100644
--- a/packages/bruno-electron/src/utils/form-data.js
+++ b/packages/bruno-electron/src/utils/form-data.js
@@ -8,9 +8,14 @@ const path = require('path');
  * @returns {string} Returns a order respecting standard compliant string of form encoded values
  */
 const buildFormUrlEncodedPayload = (params) => {
+  if (typeof params !== 'object') return '';
+  if (!Array.isArray(params)) return '';
   const resultParams = new URLSearchParams();
   for (const param of params) {
-    resultParams.append(param.name, param.value);
+    // Invalid items are ignored
+    if (typeof param !== 'object') continue;
+    if (!('name' in param)) continue;
+    resultParams.append(param.name, param.value ?? '');
   }
   return resultParams.toString();
 };


Test patch:
diff --git a/packages/bruno-electron/tests/network/prepare-request.spec.js b/packages/bruno-electron/tests/network/prepare-request.spec.js
index a0cef78e23..96af32f3cf 100644
--- a/packages/bruno-electron/tests/network/prepare-request.spec.js
+++ b/packages/bruno-electron/tests/network/prepare-request.spec.js
@@ -58,6 +58,27 @@ describe('prepare-request: prepareRequest', () => {
       const result = buildFormUrlEncodedPayload(requestObj);
       expect(result).toEqual(expected);
     });
+
+    it('returns empty string when params is not an object', () => {
+      expect(buildFormUrlEncodedPayload(null)).toEqual('');
+      expect(buildFormUrlEncodedPayload('string')).toEqual('');
+      expect(buildFormUrlEncodedPayload(123)).toEqual('');
+      expect(buildFormUrlEncodedPayload(undefined)).toEqual('');
+    });
+
+    it('ignores invalid items inside params array', () => {
+      const requestObj = [
+        { name: 'item1', value: 'a' },
+        'not-an-object',
+        { value: 'missingName' },
+        42,
+        { name: 'item2', value: 'b' },
+        { name: 'item3' }
+      ];
+      const expected = 'item1=a&item2=b&item3=';
+      const result = buildFormUrlEncodedPayload(requestObj);
+      expect(result).toEqual(expected);
+    });
   });
 
   describe.each(['POST', 'PUT', 'PATCH'])('POST request with no body', (method) => {