req.getBody()
returns string instead of JSON if body contains unquoted variable
#4364
Open
2 of 4 tasks
Labels
bug
Something isn't working
I have checked the following:
This bug is:
Bruno version
2.0.1
Operating System
Windows 11
Describe the bug
I have a request with a JSON body:
In my pre-request script,
req.getBody()
returns an object, as expected.However, if I decide to use a variable:
then
req.getBody()
suddenly starts returning a string. This can be fixed by wrapping the variable in a string:which is of course not necessarily safe, depending on how the target API works.
I understand the rationale behind returning a string: since Bruno doesn't interpolate variables before returning
req.getBody()
, the body with the unquoted, uninterpolated variable is not valid JSON (as can be seen in my second code block above, to which I didn't assign type "json" because it would highlight the incorrect syntax). Since it's not valid JSON, it can only be returned as a string.That all makes perfect sense from a Bruno developer's point of view... but from my Bruno user's point of view, before diving into this, I absolutely did not expect the type to suddenly change just because I'm using a variable.
In my opinion, this is one more reason to start interpolating values by default in
req.getBody()
(and everywhere else), as requested for instance in issue #3752 :)If that is done, then someone who explicitly requests the uninterpolated version of the body is more likely to be aware that it may not be parsable as JSON.
.bru file to reproduce the bug
Here's a collection demonstrating all 3 cases explained above (you have to use the environment, that's where the variable is defined)
test-req-body-type.zip
Screenshots/Live demo link
No screenshots
The text was updated successfully, but these errors were encountered: