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

Var used in POST's request body (JSON body) not being identified and evaluated correctly #1910

Open
nlassaux opened this issue Mar 23, 2024 · 23 comments · May be fixed by #1923
Open

Var used in POST's request body (JSON body) not being identified and evaluated correctly #1910

nlassaux opened this issue Mar 23, 2024 · 23 comments · May be fixed by #1923

Comments

@nlassaux
Copy link

nlassaux commented Mar 23, 2024

I have a Var that's declared, but I can't manage to use it as part of a json body.

The PropertyDetails Var is defined and instantiated

Now I'm trying to use it in that JSON body:

The Var is not being sent correctly

The documentation doesn't show any example for POST requests + JSON body. But do we agree that this variable should be sent in theory?

Version: v1.12.2

@dw-0
Copy link
Contributor

dw-0 commented Mar 23, 2024

Sounds a bit like the issue #1635

If possible, you could try and downgrade to v1.6.1 and try again. So you could at least verify that bug.
I tried with every version after v1.6.1 and all versions after it seem to be broken in that regard.

@nlassaux
Copy link
Author

I'm using v1.12.2, adding it to the issue's description! The new features were good though :(

@dw-0
Copy link
Contributor

dw-0 commented Mar 24, 2024

The new features were good though :(

Yes :/ i really liked them as well. But i had to downgrade as my requests really depend on that feature to work :/

@nlassaux
Copy link
Author

Thank so much for your help, downgrading as well over here then!

Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 24, 2024
Interpolate will now fallback to the original object to collect
variable values. Objets will automaticly json encoded. And if
intererpolate is executed for the json body all variables will
be json encoded so strings get double quoted.

This should fix:
- usebruno#1910
- usebruno#1635
@Its-treason
Copy link
Member

Its-treason commented Mar 24, 2024

@dw-0 @nlassaux @Nerkho @q587p I implemented a fix in my fork: https://github.com/Its-treason/bruno/releases/tag/nightly that should hopefully fix your problem. Could test it locally and give feedback on whether it fixes your Issue? I will open a PR then. The fork can safely be installed next to mainline Bruno.

@dw-0
Copy link
Contributor

dw-0 commented Mar 24, 2024

@Its-treason Sounds good! I will give it a try tomorrow and report back. Thank you.

@dw-0
Copy link
Contributor

dw-0 commented Mar 25, 2024

@Its-treason Hey! First short feedback after the first short try this morning. I wasn't able to test anything really, as the program just showed a white page when i clicked on the little eye icon on the left side of the environment dropdown. Same happend when clicked on "configure" in the env dropdown. Env variables seem to be used in the request url, but not in the body.
image
This is a login request, you can see the baseUrl is green and was correctly replaced by the value specified for it in the environment, and when hovering over it, it displays the correct value. However, it does not use the values for username and password from the environment. It also does not seem to fix the general issue mentioned in #1635 , the variables i try to set with a script, don't get send with the request body.

@Its-treason
Copy link
Member

Its-treason commented Mar 25, 2024

Hey @dw-0, thank you for testing it and the feedback

I wasn't able to test anything really, as the program just showed a white page when i clicked on the little eye icon on the left side of the environment dropdown. Same happend when clicked on "configure" in the env dropdown. Env variables seem to be used in the request url, but not in the body.

I fixed the white screen bug in the variable viewer. This happened because of changes in #650 where the collection variables were not treated as an object but as an array.

But I'm unsure about the problem with the env-variable viewer.

This is a login request, you can see the baseUrl is green and was correctly replaced by the value specified for it in the environment, and when hovering over it, it displays the correct value. However, it does not use the values for username and password from the environment. It also does not seem to fix the general issue mentioned in #1635 , the variables i try to set with a script, don't get send with the request body.

The editor does seem to recognize the collection variables, but they should be sent with the request. I used this collection for testing.

With the fork:

image

Mainline Bruno:

image

@dw-0
Copy link
Contributor

dw-0 commented Mar 25, 2024

@Its-treason Hi! You are right, with your provided collection, the environment variables are correctly interpolated. Im unsure why it did not work with my collection this morning.

But it seems like this request is not working:
test_1.zip

image
image

Am I doing something wrong? Or what is going on here? :D

What i noticed is, this is working:
image
But this is not:
image
Compare line 5 and 6 of both screenshots.

Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 25, 2024
Now the invalid JSON will be written into the body instead of the
pre interpolated body.
Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 25, 2024
Now the invalid JSON will be written into the body instead of the
pre interpolated body.
Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 25, 2024
For example with Moment the object with JSON.stringify became:
`"Mon Mar 25 2024 22:52:03 GMT+0100"` when using just `toString`
the moment object becomes `Mon Mar 25 2024 22:52:03 GMT+0100`
(without double quotes). I think the should yield more expected
results.
@Its-treason
Copy link
Member

Alright @dw-0, thank you again for testing it with my collection.

I found out why the variables were not replaced in your example. now is an Moment-Object and was JSON-stringified, but because of this was done with JSON.stringify, it added extra double quotes and the end result was Invalid JSON:

{
    "format_now": "2024-03-25 22:52:03.883",
    "nom": ""Mon Mar 25 2024 22:52:03 GMT+0100"",
    "password": "bionicman",
    "username": "dc"
}

This could be parsed, and the body data was not updated.

So I made two more changes:

  • Use toString-Method o an Object if available
    • This prevents extra double quotes to be added
  • Replace the body data with the interpolated body and then try to parse it

Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 25, 2024
Interpolate will now fallback to the original object to collect
variable values. Objets will automaticly json encoded. And if
intererpolate is executed for the json body all variables will
be json encoded so strings get double quoted.

This should fix:
- usebruno#1910
- usebruno#1635
Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 25, 2024
Now the invalid JSON will be written into the body instead of the
pre interpolated body.
Its-treason added a commit to Its-treason/bruno that referenced this issue Mar 25, 2024
For example with Moment the object with JSON.stringify became:
`"Mon Mar 25 2024 22:52:03 GMT+0100"` when using just `toString`
the moment object becomes `Mon Mar 25 2024 22:52:03 GMT+0100`
(without double quotes). I think the should yield more expected
results.
@Its-treason Its-treason linked a pull request Mar 25, 2024 that will close this issue
@dw-0
Copy link
Contributor

dw-0 commented Mar 26, 2024

@Its-treason Hi! So i tested your latest nightly today. In the morning i tested it at work, with the collection im using there. An i encountered the "white screen" issue again once i clicked on the little eye icon beside the env dropdown again. And it also appeared again, when i wanted to open the configure menu for the envs. I am not sure why this is happening only with the collection at work. Because just a minute ago i tested it with the test-collection from our conversation here, and it is working fine.
My environmets at work basically have almost the same kind of variables. Some secrets for login requests and a "baseUrl" per environment. Nothing special really. Probably hard for you to reproduce, as i definitely can't share the collection. Tomorrow i will try again and maybe delete the existing environments, and create them fresh. Maybe the collection is kind of corrupted? I don't know.

Anyways, with the current testing collection from this conversation, everything seems to work again!

@Its-treason
Copy link
Member

Its-treason commented Mar 26, 2024

Nice @dw-0, glad everything works now and thank you for testing it one last time! The white-screen issue might be related to this PR: #1932 that was merged earlier today into Bruno.

@dw-0
Copy link
Contributor

dw-0 commented Mar 26, 2024

Okay, thanks for the hint. Seems like my implementation of that feature wasn't bulletproof enough ^^
Thanks for your work!

@sanjai0py sanjai0py added the bug Something isn't working label Mar 27, 2024
@acuenca-facephi
Copy link

Some news about this?
Still having this issue with a JSON body using pre-request/collection vars.

@akhot09
Copy link

akhot09 commented Jun 27, 2024

Hi,
Do we have any further update on this issue.
I am facing similar issue wherein, when we run POST API call with JSON request body (of RAW type) , the collection variables tagged/mapped against request parameters are not been replaced with value.
The collection variables are been set using below js statement via "Pre-Request" script tab, as we are running APIs via script to get sensitive info (as a pre-condition) before running POST API call (as one of JSON request body needs that sensitive info value).
Ex: bru.setVar(envVar, value);

Please let us know when this fix will be available, as this is one of important feature which we are looking forward while working with Bruno.

@xi-ao
Copy link

xi-ao commented Jul 22, 2024

I cannot find a workaround for this bug. I'm unable to send any POST request with a JSON containing variables.
It's very annoying 😕

@amanalyre
Copy link

amanalyre commented Jul 26, 2024

The issue is still there. I was using 1.17 version, then one day it just stopped using proper variables from one of my envs with secrets. My colleague on 1.21 ver has no problems (though we share the same repo source).
The only hack that helped me to work is to downgrade to 1.6. (BTW, i'm on Mac Apple Silicon version)
@xi-ao Try also 1.6 version. You might need to manually replace secrets from env file if it disappear from the list in Bruno.

image

@timcroydon
Copy link

Can confirm issue still persists on OSX version 1.23.1

@dgtal24
Copy link

dgtal24 commented Aug 23, 2024

Issue still persists in windows version 1.26.1, variables work in TEXT mode, but not in JSON mode or in multipart form mode. In those mods the literal "{{example}}" with all the parentheses is sent.

immagine

@leikoilja
Copy link

leikoilja commented Sep 26, 2024

snap, the same here on v1.30.1 on mac silicone. Env vars using {{var}} works fine on json mode, but fails on multipart form mode and sends out literal "{{var}}"

@KriKrixs
Copy link

snap, the same here on v1.30.1 on mac silicone. Env vars using {{var}} works fine on json mode, but fails on multipart form mode and sends out literal "{{var}}"

Same here

image

@Bottleface
Copy link

I'm on v1.34.0 and this still isn't fixed. Variables are not detected correctly in JSON mode.

A workaround I found is to leave the body blank and use the req object to set the body in the pre-request scripts. You can pull the variables in via the bru object and then just use javascript to build up the body.

Not pretty, but it works.

@mesopotamiaMD
Copy link

I am on 1.38.1 already and also have the same problem. Is there any news here? thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.