-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[Bug] Extracting substring to token value doesn't reference tag properly #1809
Comments
👋 Thanks for opening your first issue! If you're reporting a 🐞 bug, please make sure To help make this a smooth process, please be sure you have first read the |
You'll have to store the response value in a temporary environment variable first. For example: {
"url": "<RESPONSE TAG>",
"code": "{{ url | replace('https://redirect.to?code=', '') }}"
} Is this for OAuth 2.0? If so, have you tried using Insomnia's built-in OAuth 2.0 support? |
Thanks @gschier, nice idea 👍 I tried this approach but unfortunately it doesn't work fully. When I create the temporary
Yes it is. I tried also the built-in OAuth 2.0 flow, but unfortunately it seems that our OAuth flow does not fully follow the standard (shame). Hopefully we'll get that fixed in the future! |
I tried to debug my configuration but couldn't make it work. Tested that when I change the Could this be a problem in insomnia? |
I spent some time investigating this one, and here are my findings. There isn't an issue with how a variable is resolved for a request, there is a more fundamental issue with resolving variables, because I managed to reproduce this bug within the environment variable editor. If the piped variable is coming from a block using {
"url": "{% response 'body', ... %}", // returns https://redirect.to?code=123
"code": "{{ url | replace('https://redirect.to?code=', '') }}" // returns 123
"use-code": "{{ code }}" // returns https://redirect.to?code=123
} And if the piped variable is coming from a regular string, we see this behavior {
"url": "https://redirect.to?code=123", // returns https://redirect.to?code=123
"code": "{{ url | replace('https://redirect.to?code=', '') }}" // returns 123
"use-code": "{{ code }}" // returns 123
} It seems there is an issue with nunjucks, when nesting I can see some interesting behavior inside the insomnia/packages/insomnia-app/app/templating/index.js Lines 42 to 46 in e485d55
|
Thanks for taking a look @develohpanda! Seeing your example environments made me remember that this is in the codebase: insomnia/packages/insomnia-app/app/common/render.js Lines 431 to 447 in f920ba1
Basically, it ensures that variables are rendered before template tags because I figured the most common use case would be to pass a variable into a tag instead of the other way around. In hindsight, my assumption might have been backwards. Either way, sorting either breaks one use case or the other. But I just had a thought. Now that we have merged your PR to preserve sort order of environment properties, we probably don't need to sort anymore. We should be able to rely on the user to define variables in preferred render order (top-to-bottom). |
Thanks @gschier, that was the pointer I needed!
I'm concerned about changing the sorting because this is likely to introduce breaking changes, but it is also a fairly specific use case. I've written a breaking test for this and updated the logic to validate it fixes the behavior, which it does, to an extent. Now, both tag ( However... the PR around preserving sort-order only kicks in while rendering the keys, and doesn't impact the order in which keys are processed through the rest of the code-base. That object loaded from the db stays as-is. While debugging, I set the keys in a particular order, but they are being processed in alphabetical order: But if I rename the keys to ensure they are processed in the correct order, things work. The direction I will go to fixing this up will be to re-order the Let me know if you have any thoughts, specifically around introducing a potentially breaking change! 😄 |
I'd need to chain certain requests. In the middle of the chain I'd need to parse a substring from a response JSON field and use that value in the next step. I couldn't find out way to do this. Is it possible somehow?
Example:
Response body
And I'd like to extract the
code
query param value from the$.url
field.The text was updated successfully, but these errors were encountered: