question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Extracting substring to token value doesn't reference tag properly

See original GitHub issue

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

{
  "url": "https://redirect.to?code=abc123"
}

And I’d like to extract the code query param value from the $.url field.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
gschiercommented, Nov 26, 2019

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?

1reaction
develohpandacommented, Dec 20, 2019

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 insomnia-plugin-response, we see this behavior

{
  "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 variable <- variable <- block, while variable <- variable <- variable works.

I can see some interesting behavior inside the templating.render, when we call the nunjucks renderString method, but I’m going in circles at this point and need to bounce some ideas I think.

https://github.com/Kong/insomnia/blob/e485d55aea4fd2edf09088feeecb4d19a1c75d7f/packages/insomnia-app/app/templating/index.js#L42-L46

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Bug] Extracting substring to token value doesn't reference tag ...
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...
Read more >
Extracting part of a string on jenkins pipeline - Stack Overflow
In this case, it looks like using a few Groovy/Java methods on the String can extract the parts. final beforeColon = url.substring(0, url....
Read more >
Allow searching for part of a word (prefix / substring searches)
In our projects we need to search for the word "ComboBox" or "Box" or just "box", but it does not retrieve the issues...
Read more >
$EXTRACT | InterSystems IRIS Data Platform 2022.2
EXTRACT - Extracts a substring from a character string by position, or replaces a substring by position.
Read more >
Everything you need to know about Regular Expressions
After reading this article you will have a solid understanding of what regular expressions are, what they can do, and what they can't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found