Insomnia: [BUG] JSONPath for chained responses doesn't work with square brackets notation; impossible to extract parameters with - in their name

Created on 16 Sep 2017  路  6Comments  路  Source: Kong/insomnia

  • Insomnia Version: 5.8.3
  • Operating System: GNU/Linux (Ubuntu 16.04.3)

Details

I am trying to add an environment variable using a chained request, I have to extract a parameter called X-BB-SESSION from a JSON response (which is inside a data object)

Because of the dashes, I can't use the simple dotted notation $.data.X-BB-SESSION, but I have to use square brackets: $.data['X-BB-SESSION']. When I write that in the edit tag prompt, the live preview works correctly and I see the value, but when I click on Done the manage environments window complains about invalid JSON (_Unexpected token ' in JSON at position .._). Similar error if I use double quotes instead of single quotes.

Taking a peek at insomnia.Environment.db, I see how the chained requests are implemented and I think the problems comes from the fact that quotes in the jsonpath are not escaped, so double quotes
break the json representation of the environment, while single quotes break the nunjucks template.

{
  "_id": "env_7819dd2f1d1b4118b6680680b5b9282e",
  "type": "Environment",
  "parentId": "wrk_456795055ea74bf2bbaeed6eb59c79a9",
  "modified": 1505559047555,
  "created": 1505505647746,
  "name":  "Base Environment",
  "data": {
    "port": 9000,
    "appcode":1234567890,
    "baasboxSession": "{% response 'body', 'req_ffca209158f844b3b2e2f8811c75f912', '$.data[###PROBLEM HERE###]' %}"
  },
  "color": null,
  "isPrivate": false
}

stale

Most helpful comment

Also, you don't have to add the escape characters in the DB file. You can add them in the edit dialog. Although, they _will_ disappear the next time you open it.

image

All 6 comments

I can confirm this issue too.

Ah yes. As you can see, Insomnia is simply using Nunjucks templating under the hood.

I'm not sure yet what a proper solution to this would look like, but you can change the quotes to escaped double quotes for now so that it will be valid JSON:

... '$.data[\"X-BB-SESSION\"]' ...

Isn't it enough to escape double quotes in the edit tag prompt? The user writes and sees ", but in the insomnia.Environment.db file Insomnia saves \".

I can confirm that manually escaping the quotes in the .db file works.

That may cover a few cases, but not all of them. For example, a user is still able to type in single quotes, which would still break things.

I think the solution to this may be to not require the pre-rendered environment to be valid JSON. That would mean that the editor could escape single quotes automatically (like you suggested with double quotes) and would also allow template variables to be used as JSON numbers/booleans (currently also not possible).

This would involve storing the environment JSON as a string in the db instead of an object, and validating the JSON after rendering, instead of before.

Also, you don't have to add the escape characters in the DB file. You can add them in the edit dialog. Although, they _will_ disappear the next time you open it.

image

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings